if n is 0:
    df.new_group.iat[0] = 1
elif grp == df.Grp.iat[n - 1]:
    df.new_group.iat[n] = df.new_group.iat[n - 1]
else:
    df.new_group.iat[n] = df.new_group.iat[n - 1] + 1
Make Pandas groupby act similarly to itertools groupby