Pandas aggregation ignoring NaN's

前端 未结 2 1803
臣服心动
臣服心动 2021-02-03 10:27

I aggregate my Pandas dataframe: data. Specifically, I want to get the average and sum amounts by tuples of [origin and type]

2条回答
  •  孤城傲影
    2021-02-03 11:21

    Use numpy's nansum and nanmean:

    from numpy import nansum
    from numpy import nanmean
    data.groupby(groupbyvars).agg({'amount': [ nansum, nanmean]}).reset_index() 
    

    As a workaround for older version of numpy, and also a way to fix your last try:

    When you do pd.Series.sum(skipna=True) you actually call the method. If you want to use it like this you want to define a partial. So if you don't have nanmean, let's define s_na_mean and use that:

    from functools import partial
    s_na_mean = partial(pd.Series.mean, skipna = True)
    

提交回复
热议问题