Pandas GroupBy.agg() throws TypeError: aggregate() missing 1 required positional argument: 'arg'

早过忘川 提交于 2019-12-09 03:16:46

问题


I’m trying to create multiple aggregations of the same field. I’m working in pandas, in python3.7. The syntax seems pretty straightforward based on the documentation:

https://pandas-docs.github.io/pandas-docs-travis/user_guide/groupby.html#named-aggregation

I do not see why I’m getting the error below. Could someone please point out the issue and tell me how to fix it?

code:

qt_dy.groupby('date').agg(std_qty=('qty','std'),mean_qty=('qty','mean'),)

error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-62-6bb3aabf313f> in <module>
      5 
      6 qt_dy.groupby('date')\
----> 7 .agg(std_qty=('qty','std'),mean_qty=('qty','mean'))

TypeError: aggregate() missing 1 required positional argument: 'arg'

回答1:


Looks like you're trying to use agg with Named aggregations—this is a supported feature from v0.25 and above ONLY.

For older versions, you will need to use the list of tuples format:

qt_dy.groupby('date')['qty'].agg([('std_qty','std'), ('mean_qty','mean')])

Or, to aggregate multiple columns, a dictionary:

qt_dy.groupby('date').agg({'qty': [('std_qty','std'), ('mean_qty','mean')]})

For more information, take a look at my answer here.



来源:https://stackoverflow.com/questions/56821579/pandas-groupby-agg-throws-typeerror-aggregate-missing-1-required-positional

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