[英]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.