Abhishek Kulkarni
Abhishek Kulkarni

Reputation: 225

Select column names where row values are not null pandas dataframe

Here is a sample dataframe:-

 A      B    C
23     45  30
54     39  NaN
NaN    45  76
87     32  NaN

I want a list (or list of lists) that contains column names where row values are not NaN.

Expected Output:-

A B C
A B
B C
A B

What is the right way of doing this? Thanking you in anticipation.

Upvotes: 2

Views: 3120

Answers (1)

anky
anky

Reputation: 75080

IIUC, you can use a .dot product of df.columns with df.notna():

df.notna().dot(df.columns+',').str.rstrip(',') #you can assign to a new column

0    A,B,C
1      A,B
2      B,C
3      A,B
dtype: object

Upvotes: 14

Related Questions