I have the following multi-level data frame:
Year 2016 2017
Quarter 3 4 1 2
Month Sep Oct Nov Dec Jan Feb Mar Apr May Jun
A 0.16 0.95 0.92 0.45 0.30 0.35 0.95 0.88 0.18 0.10
B 0.88 0.67 0.07 0.70 0.74 0.33 0.77 0.21 0.81 0.85
C 0.79 0.56 0.13 0.19 0.94 0.23 0.72 0.62 0.66 0.93
I want to sum up over the quarters, so that the final result is as follows:
Year 2016 2017
Quarter 3 4 1 2
A 0.16 2.32 1.60 1.16
B 0.88 1.44 1.85 1.86
C 0.79 0.89 1.89 2.21
I tried with the following formula:
df= df.groupby('Quarter').transform('sum')
but I get this error:
KeyError: 'Quarter'
Clearly that's the wrong way to approach it. Could anyone please a solution or to approach finding one.
Additional information
The output of the df.index
command is: Index([u'A', u'B',u'C'],dtype='object', name=u'DF name')
Thanks!
When you use groupby in pandas you group data based on columns data. But you have your groups in rows. All you need is to transpose your df before grouping and after.
Here is code you need:
First let's create df like yours:
Output:
Looks exactly like in your example. Now you need only 1 line of code:
Here's your output:
Good luck!
You can try transpose the data then
sum
it by the index you want and transpose it back the way you wantIn case you needed to pivot your results.
df.sum(level=['Year', 'Quater'], axis=1)
EDIT: thanks Matt Messersmith for note on transpose
Reproducing full example:
Just using
sum