Data frame conditional filter long list column… here is a solution to the problem.
Data frame conditional filter long list column
>>> df
c1 c2 P1
0 10.0 20.0 1
1 NaN 40.0 2
2 50.0 NaN 3
3 NaN NaN 4
4 60.0 70.0 5
5 NaN NaN 6
>>>
>>>
>>> cols = ["c1" , "c2"]
>>>
>>> df[df[cols[0]].notnull() | df[cols[1]].notnull()]
c1 c2 P1
0 10.0 20.0 1
1 NaN 40.0 2
2 50.0 NaN 3
4 60.0 70.0 5
As shown above, I’m executing a logic where I want to keep at least one row with a non-Nan value.
The problem is that the number of columns in the list may be 100 or more. So how do I execute this logic in a pythonic way?
Solution
This should work :
df.dropna(subset=cols, how='all', inplace = True)
Cols
is yours:
cols = ["c1" , "c2"]