I feel like there is a better way than this:
import pandas as pd df = pd.DataFrame( [[\'A\', \'X\', 3], [\'A\', \'X\', 5], [\'A\', \'Y\', 7], [\'A\', \'Y
use cumcount(), see docs here
cumcount()
In [4]: df.groupby(['c1', 'c2']).cumcount() Out[4]: 0 0 1 1 2 0 3 1 4 0 5 1 6 2 7 0 8 0 9 0 10 1 11 2 dtype: int64
If you want orderings starting at 1
In [5]: df.groupby(['c1', 'c2']).cumcount()+1 Out[5]: 0 1 1 2 2 1 3 2 4 1 5 2 6 3 7 1 8 1 9 1 10 2 11 3 dtype: int64