我有一个这样的数据集
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]})
我想把时间分成早餐、午餐、晚餐三餐时间。并将其添加到数据中
我是这样做的
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:
![enter image description here](https://i.stack.imgur.com/imWfo.png)