[英]How to drop the rows from dataframe that have all column values as zero or boolean false
How to drop the rows from dataframe that has all column values as zero using pandas? 如何使用pandas从所有列值为零的数据帧中删除行? In the dataframe below I use
(df.ix[:,'a':] == 0)
but how do I then vectorize testing all Boolean values for each row are False? 在下面的数据
(df.ix[:,'a':] == 0)
我使用(df.ix[:,'a':] == 0)
但是如何向量化测试每行的所有布尔值都是False?
df = pd.DataFrame({'a':[1,0,1,0], 'b':[1,0,0,0], 'c':[1,0,1,0], 'd':[1,0,0,0]}, index=['aa','bb','cc','dd'])
df.index.name = 'name'
a b c d
name
aa 1 1 1 1
bb 0 0 0 0
cc 1 0 1 0
dd 0 0 0 0
I want to drop bb and dd rows from dataframe, as all the column values are empty.Theres is way to drop columns in pandas but nothing for rows. 我想从数据帧中删除bb和dd行,因为所有列值都是空的。这是删除pandas中的列但没有行的方法。
You can use boolean indexing
with inverted mask
comparing all columns from a
with 0
, where all values in a row are True
by all(axis=1)
: 可以使用
boolean indexing
倒置mask
比较来自所有列a
与0
,其中在一行中的所有值都是True
由all(axis=1)
mask = ~(df.ix[:,'a':] == 0).all(axis=1)
print (mask)
0 True
1 False
2 True
3 False
dtype: bool
print (df[mask])
name a b c d
0 aa 1 1 1 1
2 cc 1 0 1 0
Another solution with checking at least one 1
return same output: 检查至少一个
1
另一个解决方案返回相同的输出:
print ((df.ix[:,'a':]).any(1))
0 True
1 False
2 True
3 False
dtype: bool
print (df[(df.ix[:,'a':]).any(1)])
name a b c d
0 aa 1 1 1 1
2 cc 1 0 1 0
If name
is index name and first column is index: 如果
name
是索引名称,第一列是索引:
print (df[~(df == 0).all(1)])
a b c d
name
aa 1 1 1 1
cc 1 0 1 0
Or: 要么:
print (df[df.any(1)])
a b c d
name
aa 1 1 1 1
cc 1 0 1 0
Use any()
, which returns True
iff there's any True
value (ie a value different that 0). 使用
any()
,如果有任何True
值(即不同于0的值any()
,则返回True
。
df = df[df.any(axis=1)]
As for your example, before: 至于你的例子,之前:
In[1]: df
Out[1]: a b c d
name
aa 1 1 1 1
bb 0 0 0 0
cc 1 0 1 0
dd 0 0 0 0
And after: 之后:
a b c d
name
aa 1 1 1 1
cc 1 0 1 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.