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

>Solution :

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

Leave a Reply