简体   繁体   English

如何在python中按类查找事件组的开始时间和结束时间?

[英]How to find the start time and end time of an event group by class in python?

I have a data frame consists of column 1 ie event and column 2 is Datetime:我有一个数据框由第 1 列即事件组成,第 2 列是日期时间:

Sample data样本数据

 Class Event    Time
0   A   0   2020-02-19 11:00:00
1   A   0   2020-02-19 11:30:00
2   B   1   2020-02-19 11:00:00
3   B   1   2020-02-19 11:30:00
4   B   0   2020-02-19 12:00:00
5   B   0   2020-02-19 12:30:00
6   A   0   2020-02-19 14:00:00
7   B   1   2020-02-19 13:30:00
8   A   1   2020-02-19 15:00:00
9   B   1   2020-02-19 15:30:00
10  A   0   2020-02-19 15:30:00
11  B   0   2020-02-19 16:00:00
12  A   1   2020-02-19 16:30:00

And I want to find start time and end time of each event by Class: I have tried the below code from the answer to my previous question but I am getting an empty dataframe:我想按类查找每个事件的开始时间和结束时间:我已经尝试了上一个问题的答案中的以下代码但我得到了一个空数据框:

current_event = None
result = []
grouped=df.groupby(['Class'])
for name, group in grouped:
    for class, event, time in zip(data['Class'],data['Event'], data['Time']):
        if event != current_event:
           if current_event is not None:
                result.append([Class,current_event, start_time, time])
            class, current_event, start_time = class, event, time
data = pandas.DataFrame(result, columns=['Class','Event','EventStartTime','EventEndTime'])

Desired Data所需数据

    Class   Event   EventStartTime       EventEndTime
0    A        0    2020-02-19 11:00:00  2020-02-19 15:00:00
1    A        1    2020-02-19 15:00:00  2020-02-10 15:30:00
2    A        0    2020-02-19 15:30:00  2020-02-10 16:30:00
3    B        1    2020-02-19 11:00:00  2020-02-10 12:00:00
4    B        0    2020-02-19 12:00:00  2020-02-19 13:30:00
5    B        1    2020-02-19 13:30:00  2020-02-19 16:00:00

Note: EventEndTime is time when the event changes the value say from value 1 to got change to 0 or any other value of a particular class注意:EventEndTime 是事件将值从值 1 更改为 0 或特定类的任何其他值的时间

Try:尝试:

dfg = df.groupby('Class')
df_new = pd.DataFrame()
for c,d in dfg:
    dfx = d[~(d.Event == d.shift().Event)]
    dfx['EventEndTime'] = d[~(d.Event == d.shift().Event)].shift(-1)['Time']
    dfx = dfx.dropna()
    df_new = pd.concat([df_new, dfx], ignore_index=True)
df_new.columns = ['Class','Event','EventStartTime','EventEndTime']
print(df_new)

  Class  Event      EventStartTime        EventEndTime
0     A      0 2020-02-19 11:00:00 2020-02-19 15:00:00
1     A      1 2020-02-19 15:00:00 2020-02-19 15:30:00
2     A      0 2020-02-19 15:30:00 2020-02-19 16:30:00
3     B      1 2020-02-19 11:00:00 2020-02-19 12:00:00
4     B      0 2020-02-19 12:00:00 2020-02-19 13:30:00
5     B      1 2020-02-19 13:30:00 2020-02-19 16:00:00

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中找到事件的开始时间和结束时间? - How to find the start time and end time of an event in python? 使用 Python (pandas, datetime) 在 dataframe 中查找事件(具有开始和结束时间)是否超过特定时间(例如下午 6 点) - Find whether an event (with a start & end time) goes beyond a certain time (e.g. 6pm) in dataframe using Python (pandas, datetime) 如何使用 Python 在 excel 上打印开始时间和结束时间? - How to print start time and end time on excel using Python? 如何按组计算熊猫的开始时间和结束时间之间的时差? - How to calculate time difference between start time and end time by group in pandas? 使用 Python/Pandas 以 csv 中的开始时间和结束时间日期时间列按小时分组 - Group by hour with start time and end time datetime columns in csv with Python/Pandas Python创建开始时间到结束时间之间的时隙列表 - Python create a list of time slot between start time to end time 在python中查找开始时间和结束时间之间的时隙 - finding time slots present between start time and end time in python Python - 如何在csv中使用开始和结束时间列获取列'持续时间'? - Python - How to get a column 'duration' with start and end time column in csv? 如何使用一个df的开始和结束时间范围分组并使用python从另一个df中找到每个车辆插槽的开始位置和结束位置? - how to groupby using start & end time range of one df & find the start loc & end loc of each slot of vehicles from another df using python? 在Python中调整时间列表的开始和结束范围 - Adjust start and end range for time list in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM