简体   繁体   English

Pandas pivot_table 多个 aggfunc 带边距

[英]Pandas pivot_table multiple aggfunc with margins

I've noticed that I can't set margins=True when having multiple aggfunc such as ("count","mean","sum").我注意到当有多个 aggfunc 例如(“count”,“mean”,“sum”)时,我无法设置 margins=True 。

It will vomit KeyError: 'Level None not found'它会吐出KeyError: 'Level None not found'

This is the example code.这是示例代码。

df.pivot_table(values=A,index=[B,C,D],columns=E,aggfunc=("count","mean","sum"),
margins=True,margins_name="Total",fill_value=0)

Update:更新:

This is the sample df:这是示例 df:

[{'Game_ID': 'no.1', 'Results': 0, 'Team': 'B'},
 {'Game_ID': 'no.1', 'Results': 0, 'Team': 'A'},
 {'Game_ID': 'no.1', 'Results': 0, 'Team': 'B'},
 {'Game_ID': 'no.1', 'Results': 0, 'Team': 'B'},
 {'Game_ID': 'no.1', 'Results': 1, 'Team': 'A'},
 {'Game_ID': 'no.1', 'Results': 0, 'Team': 'A'},
 {'Game_ID': 'no.1', 'Results': 0, 'Team': 'A'},
 {'Game_ID': 'no.1', 'Results': 0, 'Team': 'B'},
 {'Game_ID': 'no.1', 'Results': 0, 'Team': 'B'},
 {'Game_ID': 'no.1', 'Results': 0, 'Team': 'B'},
 {'Game_ID': 'no.1', 'Results': 1, 'Team': 'A'},
 {'Game_ID': 'no.1', 'Results': 0, 'Team': 'A'},
 {'Game_ID': 'no.1', 'Results': 1, 'Team': 'A'},
 {'Game_ID': 'no.1', 'Results': 0, 'Team': 'B'}]

And the code throwing errors.和代码抛出错误。

df.pivot_table(values="Results",index="Game_ID",
columns="Team",aggfunc=("count","mean","sum"),margins=True)

I see the error you are talking about.我看到你在谈论的错误。 I got around it by using the function calls instead of the string names "count","mean", and "sum."我通过使用函数调用而不是字符串名称“count”、“mean”和“sum”来解决这个问题。

First, we start with your dataframe:首先,我们从您的数据框开始:

import pandas as pd

df=pd.DataFrame([{'Game_ID': 'no.1', 'Results': 0, 'Team': 'B'},
 {'Game_ID': 'no.1', 'Results': 0, 'Team': 'A'},
 {'Game_ID': 'no.1', 'Results': 0, 'Team': 'B'},
 {'Game_ID': 'no.1', 'Results': 0, 'Team': 'B'},
 {'Game_ID': 'no.1', 'Results': 1, 'Team': 'A'},
 {'Game_ID': 'no.1', 'Results': 0, 'Team': 'A'},
 {'Game_ID': 'no.1', 'Results': 0, 'Team': 'A'},
 {'Game_ID': 'no.1', 'Results': 0, 'Team': 'B'},
 {'Game_ID': 'no.1', 'Results': 0, 'Team': 'B'},
 {'Game_ID': 'no.1', 'Results': 0, 'Team': 'B'},
 {'Game_ID': 'no.1', 'Results': 1, 'Team': 'A'},
 {'Game_ID': 'no.1', 'Results': 0, 'Team': 'A'},
 {'Game_ID': 'no.1', 'Results': 1, 'Team': 'A'},
 {'Game_ID': 'no.1', 'Results': 0, 'Team': 'B'}])

Then just replace the aggregate functions with standard library call to len and the numpy aggregate functions.然后只需将聚合函数替换为对len和 numpy 聚合函数的标准库调用。

  • "count" becomes len “计数”变成了len
  • "mean" becomes np.mean "mean" 变成 np.mean
  • "sum" becomes np.sum "sum" 变成 np.sum

The pivot table is made with the following lines:数据透视表由以下几行组成:

import numpy as np
df.pivot_table(values="Results",
               index="Game_ID",
               columns="Team",
               aggfunc=[len,np.mean,np.sum],
               margins=True)

输出数据透视表

Note, len might not be what you want, but in this example it gives the same answer as "count" would on its own.请注意, len可能不是您想要的,但在此示例中,它给出的答案与“count”本身的答案相同。 Look at numpy.count_nonzero , for example.例如,看看numpy.count_nonzero

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM