0
$\begingroup$

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?

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

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()

Output:

  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)})

or

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

The result should be the same.

$\endgroup$
0
$\begingroup$

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().

$\endgroup$

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