I have been banging my head around this one for some time now and can't get my head around it...
I have two Pandas Dataframes df1 and df2 which contains information that I want to summarize neatly into one.
So far, I have used an aggregate function to summarize each of these as follows:
aggregation = {'A' : {'a' : 'mean'}, 'B' : {'b' : 'mean'}, 'C' : {'c' : 'sum'}}
>> df1.groupby(by=['LEVEL_1']).agg(aggregation)
A B C
a b c
LEVEL_1
lvl_a 1.0 2.0 3.0
lvl_b 4.0 5.0 6.0
lvl_c 7.0 8.0 9.0
Same for my other DataFrame
>> df2.groupby(by=['LEVEL_1']).agg(aggregation)
A B C
a b c
LEVEL_1
lvl_a 10.0 11.0 12.0
lvl_b 13.0 14.0 15.0
lvl_c 16.0 17.0 18.0
Now, I would like to combine these two into one, total, DataFrame where my columns are grouped into the two "information universes", with an additional row totals, which is the mean of all the rows, per column, like so:
a b c
df1 df2 df1 df2 df1 df2
LEVEL_1
lvl_a 1.0 10.0 2.0 11.0 3.0 12.0
lvl_b 4.0 13.0 5.0 14.0 6.0 15.0
lvl_c 7.0 16.0 8.0 17.0 9.0 18.0
totals 4.0 13.0 5.0 14.0 6.0 15.0
There is, most likely, a supereasy way to do this, but I have not figured it out...
Thanks in advance guys.