Created
November 6, 2021 21:01
-
-
Save karpanGit/4a37158b6e395ab61d616d8c1f83dd9d to your computer and use it in GitHub Desktop.
pandas, order of groups and order within groups in groupby.py
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
# experiment with groupby cumcount and ngroup | |
import pandas as pd | |
import numpy as np | |
df = pd.DataFrame({'a':list('aaaabbba'), 'b':1}) | |
pd.concat([df.groupby(['a']).cumcount().rename('order within group'), df.groupby(['a']).ngroup().rename('group order')], axis='columns') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment