请考虑以下数据框:
test = pd.DataFrame({'A': [datetime.datetime.now(), datetime.datetime.now()], 'B': [1, 2]})
如果我像下面那样使用pivot_table
,那么一切都很好:
test.pivot_table(index = 'A', aggfunc = {'B': 'mean'}, margins = True)
但是,如果执行以下操作,则无法设置margins = True
(引发错误KeyError: 'A'
):
test.pivot_table(index = test['A'], aggfunc = {'B': 'mean'}, margins = True)
我真的很困惑。假设我需要执行以下操作,并且需要设置margin = True
。那不可能吗?
test.pivot_table(index = test['A'].dt.year, aggfunc = {'B': 'mean'}, margins = True)
答案 0 :(得分:1)
尝试:
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
因此,从技术上讲,不传递任何values
就是将所有列都传递给values
,但同时仅提供一个功能,因此这就是KeyError
的来源。
这里的源代码与文档奇怪: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.pivot_table.html