Bharat Sharma
Bharat Sharma

Reputation: 1219

dataframe conditional filter long list columns

>>> 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 am executing a logic in which i want to retain row which has atleast one non-Nan value.

Issue is that there can be as many as columns in list , like may be 100 or more . So How can i executed this logic in pythonic way?

Upvotes: 3

Views: 61

Answers (2)

Joe
Joe

Reputation: 12417

This should work:

df.dropna(subset=cols, how='all', inplace = True)

where cols is your:

cols = ["c1" , "c2"]

Upvotes: 2

Mohamed Thasin ah
Mohamed Thasin ah

Reputation: 11192

try this,

df=df.dropna(subset=[col],how='all')

Upvotes: 1

Related Questions