Say I have a dataframe such as:
df = pd.DataFrame({'A': [1, 1, 2, 3, 3, 3, 1, 1]})
I'd like to count the number of time the current column value has been seen in a row previous. For the above example, the output would be:
[1, 2, 1, 1, 2, 3, 1, 2]
I know how to group by and cumulative sum all repeating values, but I don't know how to get it to restart at each new value.
i.e.
df['A'].groupby(df['A']).cumcount()
# returns [0, 1, 0, 0, 1, 2, 2, 3] which is not what I want.