Reputation: 1
Time Shift
00:01 -6:00 First Shift
06:01 -12:00 Second Shift
12:01 -18:00 Third Shift
18:01 -24:00 Fourth Shift
Data is
DEP TIME Shift
6:00
7:45
9:50
12:05
18:20
21:15
22:55
6:00
18:45
21:30
def shift (row):
if row['DEP TIME'] >= "0:00" and row['DEP TIME'] <= "4:00":
return 'Ist'
if row['DEP TIME'] > "4:00" and row['DEP TIME'] <= "8:00" :
return '2nd'
if row['DEP TIME'] > "8:00" and row['DEP TIME'] <= "10:01":
return '3rd'
return 'Other'
df.apply (lambda row: shift (row),axis=1)
My observation is that if I give a value more than 10:00 in if condition it does not work. Is some value constraint with the data type?
Upvotes: 0
Views: 32
Reputation: 862581
Use pd.cut
for bining, but first convert values to_timedelta
:
bins = pd.to_timedelta(df1['Time'].str.split('-').str[1].add(':00'))
bins = pd.Series(pd.Timedelta(0)).append(bins)
df['DEP TIME'] = pd.to_timedelta(df['DEP TIME'].add(':00'))
df['Shift'] = pd.cut(df['DEP TIME'], bins=bins, labels=df1['Shift'])
print (df)
DEP TIME Shift
0 06:00:00 First Shift
1 07:45:00 Second Shift
2 09:50:00 Second Shift
3 12:05:00 Third Shift
4 18:20:00 Fourth Shift
5 21:15:00 Fourth Shift
6 22:55:00 Fourth Shift
7 06:00:00 First Shift
8 18:45:00 Fourth Shift
9 21:30:00 Fourth Shift
Upvotes: 1