[英]Pandas: How to group by and sum MultiIndex
I have a dataframe with categorical attributes where the index contains duplicates. 我有一个带有分类属性的数据框,其中索引包含重复项。 I am trying to find the sum of each possible combination of index and attribute. 我试图找到索引和属性的每个可能组合的总和。
x = pd.DataFrame({'x':[1,1,3,3],'y':[3,3,5,5]},index=[11,11,12,12])
y = x.stack()
print(y)
print(y.groupby(level=[0,1]).sum())
output 产量
11 x 1
y 3
x 1
y 3
12 x 3
y 5
x 3
y 5
dtype: int64
11 x 1
y 3
x 1
y 3
12 x 3
y 5
x 3
y 5
dtype: int64
The stack and group by sum are just the same. 堆栈和组合总和是一样的。
However, the one I expect is 但是,我期待的是
11 x 2
11 y 6
12 x 6
12 y 10
EDIT 2: 编辑2:
x = pd.DataFrame({'x':[1,1,3,3],'y':[3,3,5,5]},index=[11,11,12,12])
y = x.stack().groupby(level=[0,1]).sum()
print(y.groupby(level=[0,1]).sum())
output: 输出:
11 x 1
y 3
x 1
y 3
12 x 3
y 5
x 3
y 5
dtype: int64
EDIT3: An issue has been logged https://github.com/pydata/pandas/issues/10417 编辑3:已记录一个问题https://github.com/pydata/pandas/issues/10417
Using Pandas 0.15.2, you just need one more iteration of groupby 使用Pandas 0.15.2,您只需再重复一次groupby
x = pd.DataFrame({'x':[1,1,3,3],'y':[3,3,5,5]},index=[11,11,12,12])
y = x.stack().groupby(level=[0,1]).sum()
print(y.groupby(level=[0,1]).sum())
prints 版画
11 x 2
y 6
12 x 6
y 10
With pandas 0.16.2 and Python 3, I was able to get the correct result via: 使用pandas 0.16.2和Python 3,我能够通过以下方式获得正确的结果:
x.stack().reset_index().groupby(['level_0','level_1']).sum()
Which produces: 哪个产生:
0
level_0 level_1
11 x 2
y 6
12 x 6
y 10
You can then change the index and column names to more desirable ones using reindex()
and columns
. 然后,您可以使用reindex()
和columns
将索引和列名称更改为更合适的名称。
Based on my research, I agree that the failure of the original approach appears to be a bug. 根据我的研究,我同意原始方法的失败似乎是一个错误。 I think the bug is on Series
, which is what x.stack()
produces. 我认为这个bug出现在Series
,这就是x.stack()
产生的。 My workaround is to turn the Series
into a DataFrame
via reset_index()
. 我的解决方法是通过reset_index()
将Series
转换为DataFrame
。 In this case the DataFrame
does not have a MultiIndex
anymore - I'm just grouping on labeled columns. 在这种情况下, DataFrame
不再具有MultiIndex
- 我只是对标记列进行分组。
To make sure that grouping and summing works on a DataFrame
with a MultiIndex
, you can try this to get the same correct output: 要确保对具有DataFrame
的MultiIndex
进行分组和求和,您可以尝试使用它来获得相同的正确输出:
x.stack().reset_index().set_index(['level_0','level_1'],drop=True).\
groupby(level=[0,1]).sum()
Either of these workarounds should take care of things until the bug is resolved. 在解决错误之前,这些变通办法中的任何一个都应该处理好事情。
I wonder if the bug has something to do with the MultiIndex
instances that are created on a Series
vs. a DataFrame
. 我想知道这个bug是否与在Series
与DataFrame
上创建的MultiIndex
实例有关。 For example: 例如:
In[1]: obj = x.stack()
type(obj)
Out[1]: pandas.core.series.Series
In[2]: obj.index
Out[2]: MultiIndex(levels=[[11, 11, 12, 12], ['x', 'y']],
labels=[[0, 0, 1, 1, 2, 2, 3, 3], [0, 1, 0, 1, 0, 1, 0, 1]])
vs. 与
In[3]: obj = x.stack().reset_index().set_index(['level_0','level_1'],drop=True)
type(obj)
Out[3]: pandas.core.frame.DataFrame
In[4]: obj.index
Out[4]: MultiIndex(levels=[[11, 12], ['x', 'y']],
labels=[[0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 0, 1, 0, 1, 0, 1]],
names=['level_0', 'level_1'])
Notice how the MultiIndex
on the DataFrame
describes the levels more correctly. 请注意MultiIndex
上的DataFrame
如何更准确地描述级别。
sum
allows you to specify the levels to sum over in a MultiIndex data frame. sum
允许您指定要在MultiIndex数据框中求和的级别。
x = pd.DataFrame({'x':[1,1,3,3],'y':[3,3,5,5]},index=[11,11,12,12])
y = x.stack()
y.sum(level=[0,1])
11 x 2
y 6
12 x 6
y 10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.