Skip to content

Instantly share code, notes, and snippets.

@sbatururimi
Created July 10, 2022 14:23
Show Gist options
  • Save sbatururimi/c5dd304ce70fc71bd95ae1ada0acc614 to your computer and use it in GitHub Desktop.
Save sbatururimi/c5dd304ce70fc71bd95ae1ada0acc614 to your computer and use it in GitHub Desktop.
Compute percentiles per group in pandas dataframe
def percentile(n):
def percentile_(x):
return np.percentile(x, n)
percentile_.__name__ = 'percentile_%s' % n
return percentile_
df.groupby(['id'])[cols].agg(['min', 'max', np.mean, np.std, np.median, percentile(50), percentile(95)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment