Pandas pivot_table multiple aggfunc

When I create a pivot table on the data frame that I have, the transfer aggfunc='mean'works as expected, aggfunc='count'works as expected, but aggfunc=['mean', 'count']results in:AttributeError: 'str' object has no attribute '__name__

This format seemed to work earlier: Multiple AggFun in Pandas

How to create a pivot table with multiple functions?

+4
source share
2 answers

I found this to work if you just replace the square brackets with regular brackets, i.e.

aggfunc=('count','mean')
+6
source

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/

0
source

Source: https://habr.com/ru/post/1541662/


All Articles