Vb407 Vb407 - 4 months ago 8
Python Question

How to drop the rows from dataframe that has all column values as boolean false

How to drop the rows from dataframe that has all column values as zero using pandas.

name,a,b,c,d
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 cc rows from dataframe, as all the column values are empty.Theres is way to drop columns in pandas but nothing for rows.

Answer

You can use boolean indexing with inverted mask camparing all columns from a with 0, where all values are True by all:

mask = ~(df.ix[:,'a':] == 0).all(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:

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:

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