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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With