多索引DataFrame中按级别求和的列 [英] Sum columns by level in a Multi-Index DataFrame
问题描述
我的DF具有多索引列.我所有的值都在float中,并且我想在第一级多索引中合并值.请参阅下面的详细信息.
I have my df with multi-index columns. All of my values are in float, and I want to merge values with in first level of multi-index. Please see below for detail.
first bar baz foo
second one two one two one
A 0.895717 0.805244 1.206412 2.565646 1.431256
B 0.410835 0.813850 0.132003 0.827317 0.076467
C 1.413681 1.607920 1.024180 0.569605 0.875906
first bar baz foo
A (0.895717+0.805244) (1.206412+2.565646) 1.431256
B (0.410835+0.813850) (0.132003+0.827317) 0.076467
C (1.413681+1.607920) (1.024180+0.569605) 0.875906
实际上已经添加了值(我只是不想做所有这些事情:).最重要的是,我只想升级(我想是更高的级别),并在索引内添加所有值.请让我知道执行此操作的好方法.谢谢!
The values are actually added (I just didn't feel like doing all this :)). Bottom line is that I just want to level-up(higher level I guess) and within the index, add all the values. Please let me know a good way to do this. Thank you!
推荐答案
我相信您正在沿着第一个轴寻找groupby
.
I believe you're looking for a groupby
along the first axis.
df.groupby(level=0, axis=1).sum()
或者(更简洁地说)
df.sum(level=0, axis=1)
sum
的level
自变量表示分组.
The level
argument to sum
implies grouping.
df
first bar baz foo
second one two one two one two
A 2 3 3 4 10 8
B 22 16 7 3 2 26
C 4 5 1 9 6 5
df.sum(level=0, axis=1)
first bar baz foo
A 5 7 18
B 38 10 28
C 9 10 11
在性能方面,上面概述的两种方法几乎没有区别(后者快了几个刻度).
Performance wise, there's hardly any difference between the two methods outlined above (the latter is a few ticks faster).
这篇关于多索引DataFrame中按级别求和的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!