0

This is the dataframe that i have:

list = [['man walks', 'slowly', 'back'], ['slowly','None', 'None'], ['sunny','sons','None']]
df1 = pd.DataFrame(list)

which results in this dataframe

    0             1      2
0   man walks   slowly  back
1   slowly      None    None
2   sunny       sons    None

How do I add the contents of each row together ONLY IF the value is not equal to None? Desired Result is another column that looks like this:

    Sum
0  man walks slowly back 
1  slowly
2  sunny sons

Thank you so much!

0

1 Answer 1

1

Those None are actually strings in your example. Assuming they are indeed None, you can use stack (which removes missing values by default), groupby and aggregate with join:

df1.stack().groupby(level=0).agg(' '.join)

0    man walks slowly back
1                   slowly
2               sunny sons
dtype: object
Sign up to request clarification or add additional context in comments.

Comments

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.