johnJones901
johnJones901

Reputation: 47

Joining columns to create new column and adding commas unless they have commas

I have a dataframe like this:

name:  othercol:  col1:  col2:  col3: other_col:
aa         100     cc      a,     NaN      42
bb         100     a,      NaN    a,     100

I want to join all of the columns together (col1, col2, col3), separated by commas. Unless they already have a comma, and I don't want comma at the end.

Expected outcome:

name: othercol:   col1:  col2:  col3:  other_col:  output:
aa       100       cc     a,      NaN      42        cc, a
bb       100       a,    NaN      a,     100         a, a

I have tried using this method:

listy = ['col1', 'col2', 'col3', ']
df['output'] = df[listy].apply(lambda i: ', '.join(i[i.notnull()]) if str(i[:-1]) != ',' else ' '.join(i[i.notnull()]), axis = 1)

But I am getting repeated commas:

name: othercol:   col1:  col2:  col3:  other_col:  output:
aa       100       cc     a,      NaN      42        cc, a
bb       100       a,    NaN      a,     100         a,, a

Upvotes: 1

Views: 71

Answers (1)

jonny_two_knives
jonny_two_knives

Reputation: 88

Add this under your line of code and it should give you the result you want:

df['output']=df['output'].str.replace(',,',', ')

Upvotes: 1

Related Questions