Reputation: 35
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!
Upvotes: 0
Views: 301
Reputation: 88305
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
Upvotes: 1