Reputation: 25
I have a dataset with different columns: the activity description and when it's started and ended
Activity Start End In time
Activity 1 10:44:26 15:02:24
Activity 2 15:22:42 13:52:54
Activity 3 14:41:57 16:03:48
Activity 4 11:16:08 13:37:16
Activity 5 15:49:39 08:51:18
Activity 6 19:36:37 15:19:26
Activity 7 14:47:33 19:39:29
Activity 8 15:40:52 19:30:26
How can i fill in Pandas the column "In time" with this condition:
I tried with datetime module, pd.between_time()... I created my own def but it doesn't work.
How can I fix my problem?
Upvotes: 2
Views: 271
Reputation: 862641
Use numpy.where
:
#if necessary convert to times
#df['Start'] = pd.to_datetime(df['Start']).dt.time
#df['End'] = pd.to_datetime(df['End']).dt.time
from datetime import time
mask = (df.Start > time(8,0,0) ) & (df.End < time(17,30,0))
df['In time'] = np.where(mask, 'yes','no')
print (df)
Activity Start End In time
0 Activity 1 10:44:26 15:02:24 yes
1 Activity 2 15:22:42 13:52:54 yes
2 Activity 3 14:41:57 16:03:48 yes
3 Activity 4 11:16:08 13:37:16 yes
4 Activity 5 15:49:39 08:51:18 yes
5 Activity 6 19:36:37 15:19:26 yes
6 Activity 7 14:47:33 19:39:29 no
7 Activity 8 15:40:52 19:30:26 no
Upvotes: 2