Reputation: 159
The DataFrame without sum for column of a hierarchical index:
dex1 dex2 dex3 one two
H D A 1 2
B 4 5
C 7 8
I E A 1 1
B 2 2
C 3 3
The DataFrame with sum for a column of a hierarchical index should be, for columns df['one'] index dex1, 12 (1 + 4 + 7), 6 (1 + 2 + 3) and for column df['two'] of index dex1, 12 (1 + 4 + 7), 6 (1 + 2 + 3).
So far, I have tried:
df.loc[dex1].sum['one']
Upvotes: 2
Views: 588
Reputation: 862761
You can use groupby
+ GroupBy.sum
:
df1 = df.groupby(level='dex1').sum()
print (df1)
one two
dex1
H 12 15
I 6 6
From version of pandas 0.20.0
is possible omit level
parameter.
df1 = df.groupby('dex1').sum()
print (df1)
one two
dex1
H 12 15
I 6 6
Or use DataFrame.sum
with parameter level
:
df1 = df.sum(level=0)
print (df1)
one two
dex1
H 12 15
I 6 6
df1 = df.sum(level='dex1')
print (df1)
one two
dex1
H 12 15
I 6 6
Upvotes: 2