I have a sample dataset -
Id Category
1 Active
1 Active
1 Active
1 End
2 Paused
2 Active
2 Active
Expected output is a new column based on the counter which uses group by id, and resets the counter when category changes.
Expected output :-
Id Category Count
1 Active 0
1 Active 1
1 Active 2
1 End 0
2 Omitted 0
2 Active 0
2 Active 1
I have already used the following -
m = df['Category'] != df['Category'].shift(-1)
df['count'] = np.where(m, df.groupby(m.ne(m.shift(),'Id').cumsum()).cumcount()+1, 0)
but it fills with only 0
Also I have tried this -
mask = df['Id'] == df['Id'].shift(-1)
df['CatChange'] = df['Category'] != df['Category'].shift(-1)
count = df[mask].groupby('Id').cumcount()
df['CatChange_num'] = count
This just increments value without considering Category change.
Any pointers will be helpful.