Python pandas pivot_table margins keyError

余生颓废 提交于 2021-01-29 13:53:54

问题


Consider the following dataframe:

test = pd.DataFrame({'A': [datetime.datetime.now(), datetime.datetime.now()], 'B': [1, 2]})

If I use pivot_table like below then everything is fine:

test.pivot_table(index = 'A', aggfunc = {'B': 'mean'}, margins = True)

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)

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)

回答1:


Try:

test['Ax']=test['A'].dt.year

test.pivot_table(index = 'Ax' , aggfunc = 'mean', values='B', margins = True)

Outputs:

        B
Ax
2020  1.5
All   1.5

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

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.

Source here is oddly off with the documentation: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.pivot_table.html



来源:https://stackoverflow.com/questions/59869152/python-pandas-pivot-table-margins-keyerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!