Python pandas pivot_table margins keyError [英] Python pandas pivot_table margins keyError
问题描述
考虑以下数据框:
test = pd.DataFrame({'A': [datetime.datetime.now(), datetime.datetime.now()], 'B': [1, 2]})
如果我像下面那样使用 pivot_table
那么一切都很好:
If I use pivot_table
like below then everything is fine:
test.pivot_table(index = 'A', aggfunc = {'B': 'mean'}, margins = True)
但是,如果我执行以下操作,则无法设置 margins = True
(抛出错误 KeyError: 'A'
):
However, if I do the following, I can't set margins = True
(throws the error KeyError: 'A'
):
test.pivot_table(index = test['A'], aggfunc = {'B': 'mean'}, margins = True)
我真的很困惑.假设我需要执行以下操作并且需要设置 margin = True
.不可能吗?
I am really confused. Let's say I need do something like below AND need to set margin = True
. Is that impossible?
test.pivot_table(index = test['A'].dt.year, aggfunc = {'B': 'mean'}, margins = True)
推荐答案
尝试:
test['Ax']=test['A'].dt.year
test.pivot_table(index = 'Ax' , aggfunc = 'mean', values='B', margins = True)
输出:
B
Ax
2020 1.5
All 1.5
说明:如果您不传递values
,它将默认为df.columns
(数据帧的所有列,您'正在旋转).https://github.com/pandas-dev/pandas/blob/v0.25.3/pandas/core/reshape/pivot.py#L87
Explanation: in case if you don't pass values
it will default to df.columns
(all columns of dataframe, that you're pivoting over). https://github.com/pandas-dev/pandas/blob/v0.25.3/pandas/core/reshape/pivot.py#L87
所以从技术上讲,通过不传递 values
,您将所有列传递到 values
,但同时仅提供一个函数,所以这就是这个 KeyError
来自.
So technically by passing no values
you were passing ALL columns into values
, yet at the same time providing function for just one, so this is where this KeyError
was coming from.
这里的来源与文档奇怪地不同:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.pivot_table.html
Source here is oddly off with the documentation: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.pivot_table.html
这篇关于Python pandas pivot_table margins keyError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!