如何在python中找到事件的开始时间和结束时间? [英] How to find the start time and end time of an event in python?

查看:77
本文介绍了如何在python中找到事件的开始时间和结束时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由第1列即事件组成的数据框,第2列是日期时间:

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

样本数据

 Event   Time
    0   2020-02-12 11:00:00
    0   2020-02-12 11:30:00
    2   2020-02-12 12:00:00
    1   2020-02-12 12:30:00
    0   2020-02-12 13:00:00
    0   2020-02-12 13:30:00
    0   2020-02-12 14:00:00
    1   2020-02-12 14:30:00
    0   2020-02-12 15:00:00
    0   2020-02-12 15:30:00

我想找到每个事件的开始时间和结束时间:

And I want to find start time and end time of each event:

所需数据

 Event  EventStartTime  EventEndTime
    0   2020-02-12 11:00:00 2020-02-12 12:00:00
    2   2020-02-12 12:00:00 2020-02-12 12:30:00
    1   2020-02-12 12:30:00 2020-02-12 13:00:00
    0   2020-02-12 13:00:00 2020-02-12 14:30:00
    1   2020-02-12 14:30:00 2020-02-12 15:00:00

注意:EventEndTime是事件将值从1更改为0或其他任何值,反之亦然的时间

Note: EventEndTime is time when the event changes the value say from value 1 to got change to 0 or any other value or vice versa

推荐答案

假定数据框为 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.

这篇关于如何在python中找到事件的开始时间和结束时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆