Python Pandas sorting after groupby and aggregate Python Pandas sorting after groupby and aggregate pandas pandas

Python Pandas sorting after groupby and aggregate


You can use sort_values, but first reset_index and then set_index:

#simplier aggregationdays_off_yearly = persons.groupby(["from_year", "name"])['out_days'].sum()print(days_off_yearly)from_year  name 2010       John     172011       John     15           John1    182012       John     10           John4    11           John6     4Name: out_days, dtype: int64print (days_off_yearly.reset_index()                      .sort_values(['from_year','out_days'],ascending=False)                      .set_index(['from_year','name']))                 out_daysfrom_year name           2012      John4        11          John         10          John6         42011      John1        18          John         152010      John         17