2016-09-29 45 views

回答

3

可以使用boolean indexing

#convert to string and compare last value 
print ((df.Col1.astype(str).str[-1] != '0') & (df.Col1.notnull())) 
0  True 
1 False 
2  True 
3 False 
4  True 
5 False 
Name: Col1, dtype: bool 

print (df[(df.Col1.astype(str).str[-1] != '0') & (df.Col1.notnull())]) 
    Col1 
0 0.7 
2 1.1 
4 9.5 

爲轉換後的值進行比較來ìnt另一種解決方案,但是首先需要fillna

s = df.Col1.fillna(1) 
print (df[s.astype(int) != s]) 
    Col1 
0 0.7 
2 1.1 
4 9.5 

時序

#[30000 rows x 1 columns] 
df = pd.concat([df]*10000).reset_index(drop=True) 

def jez2(df): 
    s = df.Col1.fillna(1) 
    return (df[s.astype(int) != s]) 

In [179]: %timeit (df[(df.Col1.astype(str).str[-1] != '0') & (df.Col1.notnull())]) 
10 loops, best of 3: 80.2 ms per loop 

In [180]: %timeit (jez2(df)) 
1000 loops, best of 3: 1.16 ms per loop 

In [181]: %timeit (df[df.Col1 // 1 != df.Col1].dropna()) 
100 loops, best of 3: 3.04 ms per loop 

In [182]: %timeit (df[df['Col1'].mod(1) > 0].dropna()) 
100 loops, best of 3: 2.58 ms per loop 
2

使用//除法

df[df.Col1 // 1 != df.Col1].dropna() 

enter image description here

2

的另一種方法是使用mod(1)用1來計算模數:

In [60]: 
df[df['Col1'].mod(1) > 0].dropna() 

Out[60]: 
    Col1 
0 0.7 
2 1.1 
4 9.5 

在這裏我們看到的mod效果,整個數字成爲0,而小數部分仍將:

In [62]: 
df['Col1'].mod(1) 

Out[62]: 
0 0.7 
1 0.0 
2 0.1 
3 0.0 
4 0.5 
5 NaN 
Name: Col1, dtype: float64