I want to apply two different aggregates on the same column in a pandas DataFrameGroupBy and have the new columns be named.
I've tried using what is shown here in the documentation. https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html#named-aggregation
In [82]: animals.groupby("kind").agg( ....: min_height=('height', 'min'), ....: max_height=('height', 'max'), ....: average_weight=('weight', np.mean), ....: ) ....: Out[82]: min_height max_height average_weight kind cat 9.1 9.5 8.90 dog 6.0 34.0 102.75
Something like what I'm trying to do is:
df = pd.DataFrame({"year": [2001, 2001, 2001, 2005, 2005],
"value": [1, 2, 5, 3, 1]})
df = df.groupby("year").agg(sum=('value', 'sum'),
count=('value', 'size'))
However, this gives the following:
TypeError: aggregate() missing 1 required positional argument: 'arg'