Data Aggregation mission 9

Why I can’t use mean_max_dif = dif(happy_grouped) instead of mean_max_dif = happy_grouped.agg(dif). They return the same results but the system says the first one is not correct.

Hello @gaoweidong999 I hope you are familiar with OOP Concepts, agg() is a method for a class in pandas known as DataFrameGroupBy. DataFrame.groupby() returns DataFrameGroupBy, this is a groupby object that contains information about the groups. In python, method is implicitly used for an object for which it is called. that is why we use happy_grouped.agg(dif) to alter the object state.

Have you understood now?

This is a case where our answer checking isn’t refined enough to deal with multiple solutions to the same problem. Both solutions yield slightly different values due to rounding errors.

Here are the differences:

index Dataquest Alternative
1 1.1720689655172425 1.1720689655172416
3 1.0813181818181805 1.0813181818181823
6 1.4805555555555552 1.4805555555555543
8 1.2742000000000004 1.2741999999999996

As you can see, they’re rather small.

1 Like

Thanks! Does it also mean both methods are correct?

Thanks for your answer, but I don’t quite understand why we have to alter the object state just because “method is implicitly used for an object for which it is called”

Yes!

1 Like