I have a time series data which I want to find the sequence for a certain event based on the time and another category value.
Example: Time Event Category Seq 1 A 1 1 2 A 1 2 3 B 1 1 4 B 2 1 5 C 1 1 6 A 1 1
I searched and found that I can use groupby and cumcount() to do this but I'm not able to achieve the sequences at time 1 and time 6 in the above example (Event A of same category and different time should have a new sequence number, my code continues the sequence and outputs as 3)
df['sequence']=df.groupby('Event').cumcount() + 1
Related Question:
Pandas: conditional rolling count (considers the one-columnar case)