问题
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