简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM