How To Find The Start Time And End Time Of An Event In Python?
Solution 1:
Here is a method that can get the results without a for loop. I assume that the input data is read into a dataframe called df:
# Initialize the output df
dfout = pd.DataFrame()
dfout['Event'] = df['Event']
dfout['EventStartTime'] = df['Time']
Now, I create a variable called 'change' that tells you whether the event changed.
dfout['change'] = df['Event'].diff()
This is how dfout looks now:
EventEventStartTimechange002020-02-12 11:00:00 NaN102020-02-12 11:30:00 0.0222020-02-12 12:00:00 2.0312020-02-12 12:30:00 -1.0402020-02-12 13:00:00 -1.0502020-02-12 13:30:00 0.0602020-02-12 14:00:00 0.0712020-02-12 14:30:00 1.0802020-02-12 15:00:00 -1.0902020-02-12 15:30:00 0.0
Now, I go on to remove the rows where the event did not change:
dfout = dfout.loc[dfout['change'] !=0 ,:]
This will now leave me with rows where the event has changed.
Next, the event end time of the current event is the start time of the next event.
dfout['EventEndTime'] = dfout['EventStartTime'].shift(-1)
The dataframe looks like this:
EventEventStartTimechangeEventEndTime002020-02-12 11:00:00 NaN2020-02-12 12:00:00222020-02-12 12:00:00 2.02020-02-12 12:30:00312020-02-12 12:30:00 -1.02020-02-12 13:00:00402020-02-12 13:00:00 -1.02020-02-12 14:30:00712020-02-12 14:30:00 1.02020-02-12 15:00:00802020-02-12 15:00:00 -1.0NaN
You may chose to remove the 'change' column and also the last row if not needed.
Solution 2:
Assuming the dataframe is data
:
current_event =Noneresult= []
for event, timein zip(data['Event'], data['Time']):
if event != current_event:
if current_event isnotNone:
result.append([current_event, start_time, time])
current_event, start_time = event, time
data = pandas.DataFrame(result, columns=['Event','EventStartTime','EventEndTime'])
The trick is to save your event number; if the next event number is not the same as the saved one, the saved one has to be ended and a new one started.
Solution 3:
Use group by and agg to get the output in desired format.
df =pd.DataFrame([['0',11],['1',12],['1',13],['0',15],['1',16],['3',11]],columns=['Event','Time'] )
df.groupby(['Event']).agg(['first','last']).rename(columns={'first':'start-event','last':'end-event'})
Output:
Event start-eventend-event011151121631111
Post a Comment for "How To Find The Start Time And End Time Of An Event In Python?"