Skip to content Skip to sidebar Skip to footer

Merge Rows Within A Group Together

I have a pandas DataFrame where some pairs of rows have the same ID but different name. What I want is to reduce the row pair to one row, and display both of their names. INPUT: I

Solution 1:

Easily done with groupby.

df.groupby('ID', as_index=False).agg({'NAME' : ' '.join, 'AGE' : 'first'})  

ID          NAME  AGE
149          Bob   32
150  Tom Roberts   53
151       Pamela   28
152       Andrew   23

Post a Comment for "Merge Rows Within A Group Together"