Get part of day (morning, afternoon, evening, night) in Python dataframe

后端 未结 2 1151
醉酒成梦
醉酒成梦 2020-12-06 19:14

Here is my dataframe , I need to create a new column based on the timehour which the row value be like (morning, afternoon, evening, night)

Here is my code<

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-06 19:37

    After some research, this is the simplest and most efficient implementation I could find.

    prods['period'] = (prods['hour_int'].dt.hour % 24 + 4) // 4
    prods['period'].replace({1: 'Late Night',
                          2: 'Early Morning',
                          3: 'Morning',
                          4: 'Noon',
                          5: 'Evening',
                          6: 'Night'}, inplace=True)
    

    I hope this helps.

提交回复
热议问题