I want to combine rows in a pandas dataframe where a particular column contains the same value, and all the other columns, values are put into a single list.
Here's a method I came up with. First, create the sample df
df = pd.DataFrame({'Animal': ['Falcon', 'Parrot',
'Parrot', 'Falcon'],
'Max Speed': ['380.', '370.', '24.', '26.']})
df
output
Animal Max Speed
0 Falcon 380.
1 Parrot 370.
2 Parrot 24.
3 Falcon 26.
Here is the method
test = df.groupby(['Animal']).agg(lambda x: tuple(x)).applymap(list).reset_index()
test.head()
output
Animal Max Speed
0 Falcon [380., 26.]
1 Parrot [370., 24.]
Is there a more computationally efficient method for getting the same output?