Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save karpanGit/4a37158b6e395ab61d616d8c1f83dd9d to your computer and use it in GitHub Desktop.
Save karpanGit/4a37158b6e395ab61d616d8c1f83dd9d to your computer and use it in GitHub Desktop.
pandas, order of groups and order within groups in groupby.py
# 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