pandas pivot_table 多个 aggfunc

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23814605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:04:40  来源:igfitidea点击:

pandas pivot_table multiple aggfunc

pythonpandasdataframe

提问by DataSwede

When I create a pivot table on a dataframe I have, passing aggfunc='mean'works as expected, aggfunc='count'works as expected, however aggfunc=['mean', 'count']results in: AttributeError: 'str' object has no attribute '__name__

当我在我拥有的数据帧上创建数据透视表时,aggfunc='mean'按预期传递工作,按预期aggfunc='count'工作,但aggfunc=['mean', 'count']结果是:AttributeError: 'str' object has no attribute '__name__

This format seemed to work previously: Multiple AggFun in Pandas

这种格式以前似乎有效:Pandas 中的 Multiple AggFun

How do I create a pivot table with multiple functions?

如何创建具有多种功能的数据透视表?

回答by Toby Petty

I've found that this works if you simply replace square brackets with ordinary brackets i.e.

我发现如果你简单地用普通括号替换方括号即有效

aggfunc=('count','mean')

回答by ivansabik

An example:

一个例子:

In [59]: pivot_table(tips, rows=['sex', 'smoker'],
                     aggfunc={'tip_pct' : 'mean', 'size' : 'sum'})
Out[59]:
               size  tip_pct
sex    smoker
Female No      140   0.1569
       Yes     74    0.1822
Male   No      263   0.1607
       Yes     150   0.1528

Source: http://wesmckinney.com/blog/fast-and-easy-pivot-tables-in-pandas-0-5-0/

资料来源:http: //wesmckinney.com/blog/fast-and-easy-pivot-tables-in-pandas-0-5-0/