I have a DataFrame with a many-levelled MultiIndex.
I know that there are duplicates in the MultiIndex (because I don't care about a distinction that the underlying databse does care about)
I want to sum over these duplicates:
>>> x = pd.DataFrame({'month':['Sep', 'Sep', 'Oct', 'Oct'], 'day':['Mon', 'Mon', 'Mon', 'Tue'], 'sales':[1,2,3,4]})
>>> x
day month sales
0 Mon Sep 1
1 Mon Sep 2
2 Mon Oct 3
3 Tue Oct 4
>>> x = x.set_index(['day', 'month'])
sales
day month
Mon Sep 1
Sep 2
Oct 3
Tue Oct 4
To give me
day month
Mon Sep 3
Oct 3
Tue Oct 4
Buried deep in this SO answer to a similar question is the suggestion:
df.groupby(level=df.index.names).sum()
But this seems to me to fail the 'readability counts' criterion of good Python code.
Does anyone know of a more human-readable way?