Déjà vu
Déjà vu

Reputation: 783

Pandas combine multiple columns (with NoneType)

My apologies if this has been asked/answered before but I couldn't find this an answer to my problem after some time searching.

Very simply put I would like to combine multiple columns to one seperated with a , The problem is that some cells are empty (NoneType)

And when combining them I get either:

  1. TypeError: ('sequence item 3: expected str instance, NoneType found', 'occurred at index 0')

or

  1. When added .map(str), it literally adds 'None' for every NoneType value (as kinda expected)

Let's say I have a production dataframe looking like

     0        1        2
1   Rice
2   Beans    Rice
3   Milk     Beans   Rice
4   Sugar    Rice

What I would like is a single column with the values

    Production
1   Rice
2   Beans, Rice
3   Milk, Beans, Rice
4   Sugar, Rice

With some searching and tweaking I added this code:

testColumn = productionFrame.iloc[::].apply(lambda x: ', '.join(x)), axis=1)

Which produces problem 1

or changed it like this:

testColumn = productionFrame.iloc[::].apply(lambda x: ', '.join(x.map(str)), axis=1)

Which produces problem 2

Maybe it's good to add that I'm very new and kinda exploring Pandas/Python right now. So any help or push in the right direction is much appreciated!

Upvotes: 1

Views: 1347

Answers (2)

jpp
jpp

Reputation: 164773

You can use str.join after transforming NaN values to empty strings:

res = df.fillna('').apply(lambda x: ', '.join(filter(None, x)), axis=1)

print(res)

0                 Rice
1          Beans, Rice
2    Milk, Beans, Rice
3          Sugar, Rice
dtype: object

Upvotes: 0

Andrew
Andrew

Reputation: 970

pd.Series.str.cat should work here

df
Out[43]: 
       0      1     2
1   Rice    NaN   NaN
2  Beans   Rice   NaN
3   Milk  Beans  Rice
4  Sugar   Rice   NaN

df.apply(lambda x: x.str.cat(sep=', '), axis=1)
Out[44]: 
1                 Rice
2          Beans, Rice
3    Milk, Beans, Rice
4          Sugar, Rice
dtype: object

Upvotes: 2

Related Questions