I would like to create a running count of the values in the "Button" column (Start, Stop) within each "ID". However, any change in the "Button" value or a change of "ID" should reset the running count. The data frame is below:
data = pd.DataFrame({
'ID': ['A','A','B','B','C','C','C','C','C','D','E','E'],
'Button': ['Start','Stop','Start','Stop','Start','Start','Stop','Start','Stop','Start','Start','Stop']
})
I can create a running count based on the "Button" values, but can't figure out how to group that by "ID".
data['runningCount'] = data.groupby(data['Button']).cumcount()+1
I'm looking for the following result:
result = pd.DataFrame({
'ID': ['A','A','B','B','C','C','C','C','C','D','E','E'],
'Button': ['Start','Stop','Start','Stop','Start','Start','Stop','Start','Stop','Start','Start','Stop'],
'Count': [1,1,1,1,1,2,1,1,1,1,1,1]})