Similar to this Question, Given,
I would like to find the 'Cumulative Sum Of Occurrence Master Event' , and 'Cumulative Sum Of Occurrence Event A ' and Event B in between the Master Event. In other words, the instance when Master Event Occour, Cummulative Sum of Event A is reset to Zero.
Sample output is as follows,
Sample Input code by @Jon Strutz
import pandas as pd
df = pd.DataFrame({'year': [2019] * 10,
'month': [8] * 10,
'day': [16] * 10,
'hour': [12, 12, 12, 12, 13, 13, 13, 13, 13, 13],
'minute': [50, 52, 53, 57, 0, 3,4,5,13,21]})
df = pd.DataFrame(pd.to_datetime(df), columns=['Time_Stamp'])
df['Event_Master'] = [0, 0, 1, 0, 0 ,0, 0, 0, 1,0]
df['Event_B'] = [0, 0, 0, 1, 0 ,0, 1, 0, 0,1]
And expected output could be like
df['Event_Master_Out'] = [0, 0, 1, 1, 1 ,1, 1, 1, 2,2]
df['Event_B_Out'] = [0, 0, 0, 1, 1 ,1, 2, 2, 0,1]
Use Series.cumsum
and output is used for GroupBy.cumsum
:
df['Event_Master_Out'] = df['Event_Master'].cumsum()
df['Event_B_Out'] = df.groupby('Event_Master_Out')['Event_B'].cumsum()
print (df)
Time_Stamp Event_Master Event_B Event_Master_Out Event_B_Out
0 2019-08-16 12:50:00 0 0 0 0
1 2019-08-16 12:52:00 0 0 0 0
2 2019-08-16 12:53:00 1 0 1 0
3 2019-08-16 12:57:00 0 1 1 1
4 2019-08-16 13:00:00 0 0 1 1
5 2019-08-16 13:03:00 0 0 1 1
6 2019-08-16 13:04:00 0 1 1 2
7 2019-08-16 13:05:00 0 0 1 2
8 2019-08-16 13:13:00 1 0 2 0
9 2019-08-16 13:21:00 0 1 2 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.