Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter timestamp based on condition in pandas

I want to filter a timestamp column in a pandas dataframe based on a condition.

Input

Pickup date/time      
07/05/2017 09:28:00                     
14/05/2017 15:32:20                     
15/05/2018 17:00:00                    
15/05/2018 11:20:09                    
23/06/2018 22:00:00                   
29/06/2018 16:10:02  

I want to make an another column in dataframe based on a condition timestamp is greater than 16:00:00

Expected Output

   Pickup date/time      Pickup  
    07/05/2017 09:28:00      On-time                 
    14/05/2017 15:32:20      On-time             
    15/05/2018 17:00:00      Delayed              
    15/05/2018 11:20:09      On-time              
    23/06/2018 22:00:00      Delayed           
    29/06/2018 16:10:02      Delayed 
like image 762
Rahul rajan Avatar asked Aug 04 '18 13:08

Rahul rajan


Video Answer


1 Answers

Usenp.where

df['new'] = np.where(df.p.dt.hour > 16, 'Delayed', 'On-time')

or

df['new'] = np.where(df.p.dt.time > datetime.time(16, 0, 0), 'Delayed', 'On-time')

Outputs

    p                  new
0   2017-07-05 09:28:00 On-time
1   2017-05-14 15:32:20 On-time
2   2018-05-15 17:00:00 Delayed
3   2018-05-15 11:20:09 On-time
4   2018-06-23 22:00:00 Delayed
5   2018-06-29 16:10:02 Delayed
like image 84
rafaelc Avatar answered Sep 26 '22 05:09

rafaelc