gpt4 book ai didi

python - 错误无法处理 groupby 子句中的非唯一多索引是什么意思?

转载 作者:行者123 更新时间:2023-12-03 17:29:16 25 4
gpt4 key购买 nike

我有一个具有三级索引的数据框,我希望计算一个值偏离平均值的程度。但是根据我的指数,我对不同的群体有不同的平均值。这是我尝试过的:

In [4]: df['count'].groupby(level=[0,1,2]).apply(lambda x: x-np.mean(x))

但是,我收到一个错误,我在下面插入了它的堆栈跟踪。我不确定为什么会这样。
Exception                                 Traceback (most recent call last)
<ipython-input-4-678992689ff2> in <module>()
----> 1 df['count'].groupby(level=[0,1,2]).apply(lambda x: x-np.mean(x))

C:\Users\bchandra\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\groupby.pyc in apply(self, func, *args, **kwargs)
713 # ignore SettingWithCopy here in case the user mutates
714 with option_context('mode.chained_assignment',None):
--> 715 return self._python_apply_general(f)
716
717 def _python_apply_general(self, f):

C:\Users\bchandra\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\groupby.pyc in _python_apply_general(self, f)
720
721 return self._wrap_applied_output(keys, values,
--> 722 not_indexed_same=mutated)
723
724 def aggregate(self, func, *args, **kwargs):

C:\Users\bchandra\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\groupby.pyc in _wrap_applied_output(self, keys, values, not_indexed_same)
2520 if isinstance(values[0], (Series, dict)):
2521 return self._concat_objects(keys, values,
-> 2522 not_indexed_same=not_indexed_same)
2523 elif isinstance(values[0], DataFrame):
2524 # possible that Series -> DataFrame by applied function

C:\Users\bchandra\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\groupby.pyc in _concat_objects(self, keys, values, not_indexed_same)
1258
1259 if isinstance(result, Series):
-> 1260 result = result.reindex(ax)
1261 else:
1262 result = result.reindex_axis(ax, axis=self.axis)

C:\Users\bchandra\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\series.pyc in reindex(self, index, **kwargs)
2266 @Appender(generic._shared_docs['reindex'] % _shared_doc_kwargs)
2267 def reindex(self, index=None, **kwargs):
-> 2268 return super(Series, self).reindex(index=index, **kwargs)
2269
2270 @Appender(generic._shared_docs['fillna'] % _shared_doc_kwargs)

C:\Users\bchandra\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\generic.pyc in reindex(self, *args, **kwargs)
1960 # perform the reindex on the axes
1961 return self._reindex_axes(axes, level, limit, tolerance,
-> 1962 method, fill_value, copy).__finalize__(self)
1963
1964 def _reindex_axes(self, axes, level, limit, tolerance, method,

C:\Users\bchandra\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\generic.pyc in _reindex_axes(self, axes, level, limit, tolerance, method, fil
l_value, copy)
1974 new_index, indexer = ax.reindex(
1975 labels, level=level, limit=limit, tolerance=tolerance,
-> 1976 method=method)
1977
1978 axis = self._get_axis_number(a)

C:\Users\bchandra\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\index.pyc in reindex(self, target, method, level, limit, tolerance)
5280 else:
5281 raise Exception(
-> 5282 "cannot handle a non-unique multi-index!")
5283
5284 if not isinstance(target, MultiIndex):

Exception: cannot handle a non-unique multi-index!

我的数据框看起来像这样:
                 Count
Cat SubCat
1 2 7
1 2 5
1 3 4
1 3 3
4 5 2
4 5 1
4 7 0
4 7 -1

为简单起见,假设我的索引是 2 个级别而不是 3 个级别。
我想要做的是按 (cat,Sub) 分组,这意味着 (category,subcategory)。

然后在第一种情况下找到所有组的平均值,这将是 7+5/2=6,其中我按 cat=1, sub=2 分组。然后我想分别找到7-6和5-6。

所以类似 df.groupby(level=[0,1]).apply(lambda x: x-np.mean(x))
一些在我的电脑上显示错误的虚拟代码(Pandas 版本 0.17.1):
index=[]
[index.append(x) for y in range(25) for x in np.arange(2)]
subindex=[]
[subindex.append(10*x) for y in range(25) for x in np.arange(2)]
sample=pd.DataFrame({'count':np.arange(2*25),'cat':index,'sub':subindex,'date':np.random.randint(2*25)})
sample.set_index(['cat','sub'],inplace=True)
sample['count'].groupby(level=[0,1]).apply(lambda x: x-np.mean(x))

最佳答案

有一个多索引数据框,您可能没有注意到在 0、1 或 2 级存在重复的列名。这就是我遇到的情况。
如果要聚合数据,请使用以下代码计算列:
df.columns.value_counts()
然后删除重复的列。

关于python - 错误无法处理 groupby 子句中的非唯一多索引是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36062558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com