Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

OUTPUT:

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

Otherwise, I can also do ['Tom', 'Roberts'], or any other method that still captures the data.

like image 478
Landmaster Avatar asked Oct 08 '17 21:10

Landmaster


1 Answers

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
like image 71
cs95 Avatar answered Nov 15 '22 18:11

cs95