This is a trick since you are not doing this . Pandas automatically excludes numbers NaNfrom aggregation functions. Consider mine df:
b c d e
a
2 2 6 1 3
2 4 8 NaN 7
2 4 4 6 3
3 5 NaN 2 6
4 NaN NaN 4 1
5 6 2 1 8
7 3 2 4 7
9 6 1 NaN 1
9 NaN NaN 9 3
9 3 4 6 1
count() NaN, mean(). , NaN, - NaN. , NaN:
In[335]: df.groupby('a').mean()
Out[333]:
b c d e
a
2 3.333333 6.0 3.5 4.333333
3 5.000000 NaN 2.0 6.000000
4 NaN NaN 4.0 1.000000
5 6.000000 2.0 1.0 8.000000
7 3.000000 2.0 4.0 7.000000
9 4.500000 2.5 7.5 1.666667
:
In[340]: df.groupby('a')['b'].agg({'foo': np.mean})
Out[338]:
foo
a
2 3.333333
3 5.000000
4 NaN
5 6.000000
7 3.000000
9 4.500000
: , dataframe.mean API NaN, .