I am trying to sort data (Pandas) after grouping and aggregating and I am stuck. My data:
data = {\'from_year\': [2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012]
You can use sort_values, but first reset_index
and then set_index
:
#simplier aggregation
days_off_yearly = persons.groupby(["from_year", "name"])['out_days'].sum()
print(days_off_yearly)
from_year name
2010 John 17
2011 John 15
John1 18
2012 John 10
John4 11
John6 4
Name: out_days, dtype: int64
print (days_off_yearly.reset_index()
.sort_values(['from_year','out_days'],ascending=False)
.set_index(['from_year','name']))
out_days
from_year name
2012 John4 11
John 10
John6 4
2011 John1 18
John 15
2010 John 17