Scope
Scope

Reputation: 789

How to achieve this concatenation in Pandas?

I have a dataframe df :-

tray bag ball
0 1 1
0 1 0
1 1 0
0 0 1
0 0 0

I want to add a column Presence in the dataframe df seperated by comma this way :-

tray bag ball Presence
0 1 1 bag,ball
0 1 0 bag
1 1 0 tray,bag
0 0 1 ball
0 0 0 No Presence

Upvotes: 0

Views: 37

Answers (1)

jezrael
jezrael

Reputation: 862541

Use DataFrame.dot with mask - compare columns by 1 with columns names and separator, last replace empty string:

df['Presence'] = df.eq(1).dot(df.columns + ',').str[:-1].replace('','No Presence')
print (df)
   tray  bag  ball     Presence
0     0    1     1     bag,ball
1     0    1     0          bag
2     1    1     0     tray,bag
3     0    0     0  No Presence

EDIT: If use Series.str.extract for values before _ add expand=False for avoid one column DataFrame:

print (df)
   tray_col  bag  ball
0         0    1     1
1         0    1     0
2         1    1     0
3         0    0     0

df['Presence'] = (df.eq(1)
                    .dot(df.columns.str.extract(r'^([^_]+)', expand=False) + ',')
                    .str[:-1].replace('','No Presence'))
print (df)
   tray_col  bag  ball     Presence
0         0    1     1     bag,ball
1         0    1     0          bag
2         1    1     0     tray,bag
3         0    0     0  No Presence

Upvotes: 1

Related Questions