简体   繁体   English

如何从数据框中删除所有列值为零或布尔值为false的行

[英]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比较来自所有列a0 ,其中在一行中的所有值都是Trueall(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.

相关问题 如何删除包含所有零值的行但不使用非零值的零 - How to drop rows with ALL zero values but not zeros WITH non zero values 从数据框中删除布尔行 - Drop boolean rows from dataframe 如何根据特定列中的空值从数据框中删除行? - How to drop rows from a dataframe as per null values in a specific column? 如何通过列值的条件删除 DataFrame 中的行 - How to Drop rows in DataFrame by conditions on column values 如何删除 DataFrame 中指定列中该行中有 NAN 的所有行? - How do I drop all rows in a DataFrame that have NAN in that row, in a specified column? 如何通过熊猫或火花数据框删除所有行中具有相同值的列? - How to drop columns which have same values in all rows via pandas or spark dataframe? 如果列行中的值为零,则删除 pandas dataframe 中所有列中的所有行 - Drop all rows in all columns in pandas dataframe if the value in the column row is zero 如果 dataframe 2 的 A 列与 pandas 的 A 列不同,如何将 dataframe 1 的 A 列中的值归零? - How to zero values in column A of dataframe 1 if they are different from column A of dataframe 2 in pandas? 如何从 DataFrame 中删除某些列只有零值的行 - How to remove rows from a DataFrame where some columns only have zero values 如何将值设置为布尔过滤的dataframe列的行 - how to set values to rows of boolean filtered dataframe column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM