Python 对熊猫系列中的多索引级别求和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24826368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:17:38  来源:igfitidea点击:

Summing over a multiindex level in a pandas series

pythonpandasstatisticsmulti-index

提问by dylkot

Using the Pandas package in python, I would like to sum (marginalize) over one level in a series with a 3-level multiindex to produce a series with a 2 level multiindex. For example, if I have the following:

在 python 中使用 Pandas 包,我想对具有 3 级多索引的系列中的一个级别求和(边缘化)以生成具有 2 级多索引的系列。例如,如果我有以下内容:

ind = [tuple(x) for x in ['ABC', 'ABc', 'AbC', 'Abc', 'aBC', 'aBc', 'abC', 'abc']]
mi = pd.MultiIndex.from_tuples(ind)
data = pd.Series([264, 13, 29, 8, 152, 7, 15, 1], index=mi)

A  B  C    264
      c     13
   b  C     29
      c      8
a  B  C    152
      c      7
   b  C     15
      c      1

I would like to sum over the variable C to produce the following output:

我想对变量 C 求和以产生以下输出:

A  B    277
   b     37
a  B    159
   b     16

What is the best way in Pandas to do this?

Pandas 中执行此操作的最佳方法是什么?

采纳答案by chrisaycock

If you know you always want to aggregate over the first two levels, then this is pretty easy:

如果你知道你总是想聚合前两个级别,那么这很容易:

In [27]: data.groupby(level=[0, 1]).sum()
Out[27]:
A  B    277
   b     37
a  B    159
   b     16
dtype: int64