# Skip NA in Mean function within Pandas agg function [closed]

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?

• From the docs: "NA groups in GroupBy are automatically excluded". – 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()


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.

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