Pandas aggregation ignoring NaN's

前端 未结 2 1802
臣服心动
臣服心动 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:00

    It might be too late but anyways it might be useful for others.

    Try apply function:

    import numpy as np
    import pandas as pd
    
    def nan_agg(x):
        res = {}
    
        res['nansum'] = x.loc[ not x['amount'].isnull(), :]['amount'].sum()
        res['nanmean'] = x.loc[ not x['amount'].isnull(), :]['amount'].mean()
    
        return pd.Series(res, index=['nansum', 'nanmean'])
    
    result = data.groupby(groupbyvars).apply(nan_agg).reset_index() 
    

提交回复
热议问题