Issue
In the pandas package of python, how would one do the following the most easily?
df = pd.DataFrame({'Animal': ['Falcon', 'Falcon',
'Parrot', 'Parrot'],
'Max Speed': [380., 370., 24., 26.]})
Resulting in:
Animal Max Speed
0 Falcon 380.0
1 Falcon 370.0
2 Parrot 24.0
3 Parrot 26.0
Converting this into:
Falcon Parrot
0 380 24
1 370 26
I would have expected to able to use the groupby method, but I cannot seem to figure out how to create a new dataframe out of that.
df.groupby('Animal').to_frame() or something along those lines
edit: okay after some messing about I managed to find a solution in the form of:
df.groupby('Animal')['Max Speed'].apply(pd.DataFrame).apply(lambda x: pd.Series(x.dropna().to_numpy()))
But that seems to be quite clumsy, there is bound to be a better way right?
Solution
You can aggregate the values in lists during the groupby, convert to a dictionary, and then back to a DataFrame:
pd.DataFrame(dict(df.groupby('Animal')['Max Speed'].apply(list)))
Answered By - Arne
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.