[英]how to see if datetime is between start and end time of different dataframe in pandas
I have following two dataframes in pandas 我在熊猫中有以下两个数据框
df
code start_time end_time start end flag
0 2018-08-01 06:30:00 2018-08-01 06:40:00 123 145 0
1 2018-08-01 06:40:00 2018-08-01 06:50:00 145 150 0
2 2018-08-01 07:10:00 2018-08-01 07:20:00 155 160 1
df1
code occur_time Ack_time Alarm
0 2018-08-01 06:50:00 2018-08-01 08:00:00 437
1 2018-08-01 06:40:00 2018-08-01 07:10:00 430
2 2018-08-01 07:10:00 2018-08-01 07:20:00 456
The condition I want to check is,in df
dataframe flag
should be 1 then for that perticular row start_time
should be between occur_time
and Ack_time
and Alarm
should be 437
我要检查的条件是,在df
dataframe flag
应该为1,然后对于该特定行, start_time
应该在occur_time
和Ack_time
之间, Alarm
应该为437
My desired dataframe would be 我想要的数据框是
df
code start_time end_time start end flag manual
0 2018-08-01 06:30:00 2018-08-01 06:40:00 123 145 0 1
1 2018-08-01 06:40:00 2018-08-01 06:50:00 145 150 0 0
2 2018-08-01 07:10:00 2018-08-01 07:20:00 155 160 1 0
Try: 尝试:
df.loc[:, 'manual'] = (df1.occur_time.between(df.start_time, df.end_time) \
& (df1.Alarm == 437) \
& df.flag).apply(int)
EDIT: Added checking the flag also. 编辑:还添加了检查标志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.