2

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.

1 Answer 1

14

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
Sign up to request clarification or add additional context in comments.

1 Comment

Man wish I could upvote this answer more than once. Smart way of using the matrix multiplication.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.