I have a dataset like this:
data = pd.DataFrame({'time':['13:30', '9:20', '18:12', '19:00', '8:20']})
I split the time to get the hour
part only and add it to the table.
data['hour'] = data.Time.apply(lambda x: int(x.split(':')[0]))
Now I want to split the time into 3 parts of the day and 3 meal times, assume the hour interval (6,11]
is for breakfast, (11,15]
for lunch, and (15,20]
for dinner.
I tried this code but seems to not working.
def time_period(hour):
if hour >= 6 and hour < 11:
return 'breakfast'
elif hour >= 11 and hour < 15:
return 'lunch'
else:
return 'dinner'
time = [time_period(y) for y in hours]
date['meal'] = time
question from:
https://stackoverflow.com/questions/65907592/convert-string-to-timestamp-and-split-the-data 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…