Skip to content

Instantly share code, notes, and snippets.

@pierdom
Created October 4, 2018 13:15
Show Gist options
  • Save pierdom/2d498aca7c24f0116dd2790565c7d1b6 to your computer and use it in GitHub Desktop.
Save pierdom/2d498aca7c24f0116dd2790565c7d1b6 to your computer and use it in GitHub Desktop.
[Passing custom function to agg function in Pandas (with name!)] #pandas #datascience
# define a custom function that calculates the percentile using numpy
def perc(n):
def perc_(x):
return np.percentile(x, n)
perc_.__name__ = 'perc_%s' % n
return perc_
# apply this function (and other stuff) to Pandas Groupby
df.groupby(['group_key_1', 'group_key_2']).agg({
'col_a': np.sum,
'col_b': [np.average, np.sum],
'col_c': [
perc(25), # the column name will be perc_25
perc(50), # the column name will be perc_50
perc(75), # the column name will be perc_75
perc(99), # the column name will be perc_99
np.average],
'col_d' : lambda x: x + 1 # the problem with this is that it won't have a name
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment