How to find the start time and end time of an event in python?

后端 未结 3 1059
隐瞒了意图╮
隐瞒了意图╮ 2021-01-27 11:22

I have a data frame consists of column 1 i.e event and column 2 is Datetime:

Sample data

 Event   Time
    0   2020-02-12 11:00:00
    0   2020-02-12 11         


        
3条回答
  •  有刺的猬
    2021-01-27 12:18

    Assuming the dataframe is data:

    current_event = None
    result = []
    for event, time in zip(data['Event'], data['Time']):
        if event != current_event:
            if current_event is not None:
                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.

提交回复
热议问题