Enumerate each row for each group in a DataFrame
There’s cumcount, for precisely this case: df[‘col_c’] = g.cumcount() As it says in the docs: Number each item in each group from 0 to the length of that group – 1. Original answer (before cumcount was defined). You could create a helper function to do this: def add_col_c(x): x[‘col_c’] = np.arange(len(x)) return x First sort … Read more