I have a data set like this
df = pd.DataFrame({'time':['13:30', '9:20', '18:12', '19:00', '11:20', '13:30', '15:20', '17:12', '16:00', '8:20'],
'item': [coffee, bread, pizza, rice, soup, coffee, bread, pizza, rice, soup]})
I want to split the time into 3 meal times breakfast, lunch, dinner. and add it to data
I did it like this
df['hour'] = df.Time.apply(lambda x: int(x.split(':')[0]))
def time_period(hour):
if hour >= 6 and hour < 11:
return 'breakfast'
elif hour >= 11 and hour < 15:
return 'lunch'
else:
return 'dinner'
df['meal'] = df['hour'].apply(lambda x: time_period(x))
now I want to groupby the data based on these 3 meals and have an output like this:
question from:
https://stackoverflow.com/questions/65908850/group-by-two-columns-based-on-created-column 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…