SHARE
TWEET

Untitled

a guest Jun 26th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. import pandas as pd
  3. result = data.groupby(groupbyvars).agg({'amount': [ pd.Series.sum, pd.Series.mean]}).reset_index()
  4.      
  5. data.groupby(groupbyvars).agg({'amount': [ pd.Series.sum(skipna=True), pd.Series.mean(skipna=True)]}).reset_index()
  6.      
  7. s_na_mean = partial(pd.Series.mean, skipna = True)    
  8. data.groupby(groupbyvars).agg({'amount': [ np.nansum, s_na_mean ]}).reset_index()
  9.      
  10. error: 'functools.partial' object has no attribute '__name__'
  11.      
  12. from numpy import nansum
  13. from numpy import nanmean
  14. data.groupby(groupbyvars).agg({'amount': [ nansum, nanmean]}).reset_index()
  15.      
  16. from functools import partial
  17. s_na_mean = partial(pd.Series.mean, skipna = True)
  18.      
  19. import numpy as np
  20. import pandas as pd
  21.  
  22. def nan_agg(x):
  23.     res = {}
  24.  
  25.     res['nansum'] = x.loc[ not x['amount'].isnull(), :]['amount'].sum()
  26.     res['nanmean'] = x.loc[ not x['amount'].isnull(), :]['amount'].mean()
  27.  
  28.     return pd.Series(res, index=['nansum', 'nanmean'])
  29.  
  30. result = data.groupby(groupbyvars).apply(nan_agg).reset_index()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top