Neo
Neo

Reputation: 776

How to merge multiple rows based on a single column (implode or nest) in pandas dataframe?

I'm looking to combine multiple row in a dataframe into a single row based on one column

This is what my df looks like:

    id       Name       score
0  1234      jim         34
1  5678      james       45
2  4321      Macy        56
3  1234      Jim         78
4  5678      James       80

I want to combine based on column "score" so the output would look like:

    id       Name       score
0  1234      jim         34,78
1  5678      james       45,80
2  4321      Macy        56

Basically I want to do the reverse of the explode function. How can I achieve this using pandas dataframe?

Upvotes: 3

Views: 1445

Answers (1)

BENY
BENY

Reputation: 323306

Try agg with groupby

out = df.groupby('id',as_index=False).agg({'Name':'first','score':lambda x : ','.join(x.astype(str))})
Out[29]: 
     id   Name  score
0  1234    jim  34,78
1  4321   Macy     56
2  5678  james  45,80

Upvotes: 6

Related Questions