1

I have this Dataframe pd

    Id   Month    gender    grade
    1     Mai     F           2
    2     Juin    M           3
    2     Juin    M           4

I want to have this Output

    Id   Month      gender    grade    
    1     Mai          F         2
    2     Juin         M         3, 4   
    

Any ideas please ?

Karla
  • 11
  • 3
  • 1
    Something like this: `df.groupby(['Id','Month','gender'],as_index=False)['grade'].agg(lambda x: ','.join(x.map(str)))` should do – anky Jul 19 '21 at 08:45

1 Answers1

0

Try using groupby with agg:

df['grade'] = df['grade'].astype(str)
print(df.groupby(['Id', 'Month', 'gender'], as_index=False).agg(','.join))

Output:

   Id Month gender grade
0   1   Mai      F     2
1   2  Juin      M   3,4
U12-Forward
  • 65,118
  • 12
  • 70
  • 89