Home > Software design >  How to achieve this concatenation in Pandas?
How to achieve this concatenation in Pandas?

Time:01-18

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

CodePudding user response:

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
  • Related