如何在数据框的某些列中过滤零行?

时间:2014-08-22 07:57:39

标签: python-2.7 pandas

在pandas中,如何在某些列中过滤掉值为零的行? 我需要删除那些行,其中所有值(第一列除外)都为零。

1 个答案:

答案 0 :(得分:2)

In [70]:
# construct some dummy data
df = pd.DataFrame({'a':randn(5), 'b':[1,2,1,0,0], 'c':[0,0,0,0,0], 'd':[0,0,0,0,1]})
df
Out[70]:
          a  b  c  d
0 -1.125360  1  0  0
1 -0.485210  2  0  0
2 -1.461206  1  0  0
3 -0.121767  0  0  0
4  0.168165  0  0  1

In [82]:

# mask where values are not 0
mask = df[df.drop('a', axis=1) != 0]
mask
Out[82]:
    a   b   c   d
0 NaN   1 NaN NaN
1 NaN   2 NaN NaN
2 NaN   1 NaN NaN
3 NaN NaN NaN NaN
4 NaN NaN NaN   1

In [94]:
# drop NaN values with a threshold of 1 valid value, and use the index to select those rows    
df.loc[mask.dropna(thresh=1).index]
Out[94]:
          a  b  c  d
0 -1.125360  1  0  0
1 -0.485210  2  0  0
2 -1.461206  1  0  0
4  0.168165  0  0  1