简体   繁体   中英

pandas pivot_table multiple aggfunc

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__

This format seemed to work previously: Multiple AggFun in Pandas

How do I create a pivot table with multiple functions?

我发现如果你只是用普通的方括号代替方括号,那就行了

aggfunc=('count','mean')

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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