grouping and summing multiindex dataframe in pandas
<p>I have a dataframe like this.            </p>

<pre><code>df1=pd.DataFrame({"A":np.random.randint(1,10,4),"B":np.random.randint(1,10,4),"C":list('abba')})
df1.index.name="first"    
df2=pd.DataFrame({"A":np.random.randint(1,10,5),"B":np.random.randint(1,10,5),"C":list('aaabb')})
df2.index.name="second"
df=pd.concat([df1,df2], keys=['first', 'second'])
df
          A  B  C
first  0  6  5  a
       1  2  2  b
       2  1  6  b
       3  6  9  a
second 0  6  6  a
       1  9  9  a
       2  8  4  a
       3  7  2  b
       4  9  8  b
</code></pre>

<p>I would like to get grouping and summing result like this.
the (key= column "C")</p>

<pre><code>    first        second
    A      B     A       B
a   15     14    23      19
b   3      8     16      10
</code></pre>

<p>How can I get this result ?</p>

<p>One way to do this would be:</p>

<pre><code>In [126]: df1=pd.DataFrame({"A":np.random.randint(1,10,4),"B":np.random.randint(1,10,4),"C":list('abba')})

In [127]: df2=pd.DataFrame({"A":np.random.randint(1,10,5),"B":np.random.randint(1,10,5),"C":list('aaabb')})

In [128]: df1
Out[128]: 
   A  B  C
0  7  9  a
1  1  3  b
2  7  7  b
3  1  2  a

In [129]: df2
Out[129]: 
   A  B  C
0  3  1  a
1  3  1  a
2  7  3  a
3  9  7  b
4  9  1  b

In [130]: df = pd.concat({"first": df1.groupby('C').sum(), "second": df2.groupby('C').sum()}, axis = 1)

In [131]: del df.index.name

In [132]: df
Out[132]: 
  first     second   
      A   B      A  B
a     8  11     13  5
b     8  10     18  8
</code></pre>

python pandas