pandas: sorting observations within groupby groups

Viewed 30898

According to the answer to pandas groupby sort within groups, in order to sort observations within each group one needs to do a second groupby on the results of the first groupby. Why a second groupby is needed? I would've assumed that observations are already arranged into groups after running the first groupby and all that would be needed is a way to enumerate those groups (and run apply with order).

2 Answers

They need a second group by in that case, because on top of sorting, they want to keep only the top 3 rows of each group.

If you just need to sort after a group by you can do :

df_res = df.groupby(['job','source']).agg({'count':sum}).sort_values(['job','count'],ascending=False)

One group by is enough.

And if you want to keep the 3 rows with the highest count for each group, then you can group again and use the head() function :

df_res.groupby('job').head(3)
Related