Step2, Course 4/6: Data Cleaning and Analysis, Section: Data Aggregation, Step: 9/14


My question regarding the example from Course.

Why do we need to use GroupBy.agg() method if we have only one function for aggregate?
In case if we have only one function, we don’t need to use .agg method to get results.

In given example both methods gives the same answer, but only first accepted as correct:
mean_max_dif1 = happy_grouped.agg(dif)
mean_max_dif2 = dif(happy_grouped)

Initially, based on name of the results, I thought the question was to aggregate three functions: Mean, Max and Diff. And probably this would be better example to demonstrate the .agg method functionality.


groupby.agg allows you to feed a dictionary in, with key being name of resulting column, value being the function. If you just use function alone, the resulting column name would be the function name

Thank you, understood.
I think this example will be more valuable if it will demonstrate .agg method functionality to combine user defined function (dif in that case) and existing np.max, np.min, np.mean methods.