Here's my working code:

new_df = old_df.groupby('store', as_index = False).agg({'reviewScore': 'mean'})

I would like to add an argument to the mean function: skipna=True

I can't figure out the syntax to make it happen.

Do I need to learn how to use lambda functions?

  • $\begingroup$ From the docs: "NA groups in GroupBy are automatically excluded". $\endgroup$ – desertnaut Oct 28 at 10:57

NaN values are skipped automatically. For example if we have the following table:

  store  reviewScore
0     a          4.0
1     b          3.0
2     a          3.0
3     a          NaN
4     b          5.0

groupby and find mean for each group:

df.groupby('store', as_index = False).agg({'reviewScore': 'mean'})

what is equivalent to:

df.groupby('store', as_index = False).mean()


  store  reviewScore
0     a          3.5
1     b          4.0

To use arguments in aggregation functions you can utilize a lambda function:

df.groupby('store', as_index = False).agg({'reviewScore': lambda x: x.mean(skipna=True)})


df.groupby('store', as_index = False).agg(lambda x: x.mean(skipna=True))

The result should be the same.

| improve this answer | |

The internal mean() function will ignore NaN values.

The only scenario well you get NaN, is when NaN is the only value. Then, the mean value of an empty set, gives NaN.

Aggregate functions agg work in the same way as mean().

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.