Summing rows in grouped pandas dataframe and return NaN
I'm not sure where this falls on the ugliness scale, but it works:
>>> series_sum = pd.core.series.Series.sum
>>> df.groupby('l')['v'].agg(series_sum, skipna=False)
l
left -3
right NaN
Name: v, dtype: float64
I just dug up the sum
method you used when you took df.v.sum
, which supports the skipna
option:
>>> help(df.v.sum)
Help on method sum in module pandas.core.generic:
sum(axis=None, skipna=None, level=None, numeric_only=None, **kwargs) method
of pandas.core.series.Series instance
I think it's inherent to pandas. A workaround can be :
df.groupby('l')['v'].apply(array).apply(sum)
to mimic the numpy way,
or
df.groupby('l')['v'].apply(pd.Series.sum,skipna=False) # for series, or
df.groupby('l')['v'].apply(pd.DataFrame.sum,skipna=False) # for dataframes.
to call the good function.