Skip to content

Instantly share code, notes, and snippets.

@maneeshdisodia
Last active January 4, 2019 10:45
Show Gist options
  • Save maneeshdisodia/8ac952071337548a6acc615cf54a07c5 to your computer and use it in GitHub Desktop.
Save maneeshdisodia/8ac952071337548a6acc615cf54a07c5 to your computer and use it in GitHub Desktop.
aggregation in pandas groupby
#create fake data example taken from stackoverflow
df_example = pd.DataFrame({'CG':np.random.randint(0, 5, 100), 'Morph':np.random.choice(['S', 'E'], 100), 'R':np.random.rand(100) * -100})
def my_agg(x):
x = x.sort_values('R')
morph = x.head(1)['Morph'].values[0]
diff = x.iloc[0]['R'] - x.iloc[1]['R']
diff2 = -2.5*np.log10(sum(10**(-0.4*x['R'])))
prop = (x['Morph'].iloc[1:] == 'S').mean()
return pd.Series([morph, diff, diff2, prop], index=['morph', 'diff', 'diff2', 'prop'])
df_example.groupby('CG').apply(my_agg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment