Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete rows from pandas dataframe if all its columns have empty string

I have a dataframe as follows

    Name Age
0    Tom  20
1   nick  21
2           
3  krish  19
4   jack  18
5           
6   jill  26
7   nick

Desired output is

    Name Age
0    Tom  20
1   nick  21
3  krish  19
4   jack  18
6   jill  26
7   nick

The index should not be changed and if possible would be nice if I don't have to convert empty strings to NaN. It should be removed only if all the columns have '' empty strings

like image 492
Arteezy Avatar asked Oct 19 '25 02:10

Arteezy


1 Answers

You can do:

# df.eq('') compare every cell of `df` to `''`
# .all(1) or .all(axis=1) checks if all cells on rows are True
# ~ is negate operator.
mask = ~df.eq('').all(1)

# equivalently, `ne` for `not equal`, 
# mask = df.ne('').any(axis=1)

# mask is a boolean series of same length with `df`
# this is called boolean indexing, similar to numpy's
# which chooses only rows corresponding to `True`
df = df[mask]

Or in one line:

df = df[~df.eq('').all(1)]
like image 52
Quang Hoang Avatar answered Oct 21 '25 16:10

Quang Hoang