Skip to content

Instantly share code, notes, and snippets.

@kaizer1v
Last active July 19, 2021 03:56
Show Gist options
  • Save kaizer1v/885855d794b3ddea1b580206c05336dc to your computer and use it in GitHub Desktop.
Save kaizer1v/885855d794b3ddea1b580206c05336dc to your computer and use it in GitHub Desktop.
Top 5 in every group - Pandas
# Data Columns available are = ['track', 'year', 'minutes_streamed']
# GOAL: Top 5 tracks for every year from 2010 to 2020 based on the number of minutes streamed
group_1 = df.groupby(['year', 'track']).agg({'minutes_streamed': sum})
group_1['minutes_streamed'].groupby(['year'], group_keys=False).nlargest(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment