Created
August 21, 2018 13:53
-
-
Save ian-whitestone/b913dadf35d7da2dca1d264001ff6cb2 to your computer and use it in GitHub Desktop.
Pandas equivalent of SQL's row_number()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Let's add an row number to indicate the first message per app & microservice | |
# This code is analagous to the SQL: row_number() over (partition by id, topic order by msg_ts asc) | |
df['row_num'] = df.sort_values(['id', 'msg_ts'], ascending=True).groupby(['id', 'topic']).cumcount() + 1 |
now i want to select all the rows in each group that has row_num=1
df[df.row_num == 1]
Thanks it worked
Thank you.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
now i want to select all the rows in each group that has row_num=1