3
Ich versuche, Daten (Pandas) nach der Gruppierung und Aggregation zu sortieren, und ich bin fest. Meine Daten:Python Pandas sortieren nach groupby und aggregieren
data = {'from_year': [2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012],
'name': ['John', 'John1', 'John', 'John', 'John4', 'John', 'John1', 'John6'],
'out_days': [11, 8, 10, 15, 11, 6, 10, 4]}
persons = pd.DataFrame(data, columns=["from_year", "name", "out_days"])
days_off_yearly = persons.groupby(["from_year", "name"]).agg({"out_days": [np.sum]})
print(days_off_yearly)
Danach habe ich meine Daten sortiert:
out_days
sum
from_year name
2010 John 17
2011 John 15
John1 18
2012 John 10
John4 11
John6 4
Ich möchte, dass meine Daten von from_year und out_days Summe und erwartete Daten sortieren sein:
out_days
sum
from_year name
2012 John4 11
John 10
John6 4
2011 John1 18
John 15
2010 John 17
I versuche
print(days_off_yearly.sort_values(["from_year", ("out_days", "sum")], ascending=False).head(10))
Aber KeyError bekommen: 'from_year'.
Jede Hilfe wird geschätzt.