Python - dropping rows based on multiple filters

Viewed 31

quick question on how to remove rows that have multiple blanks opposed to one at a time?

for example:

1 2 3 4
X X X
X X X X
X
X X
X X X X
X X X X
X

My DataFrame has 20k entries so manually parsing it out is not possible. I would like to write a script that basically does something similar to an excel script that basically says:

If 2,3 and 4 are empty, drop row

Thanks!

2 Answers
df.dropna(thresh=2)

This will drop all rows which have 3 or more NAs

boolean mask

df[(~df.astype(bool)).sum(axis=1).le(2)]

   1  2  3  4
0  X  X  X   
1  X  X  X  X
3  X  X      
4  X  X  X  X
5  X  X  X  X
Related