Time into duration/Session using Python

前端 未结 1 1444
执念已碎
执念已碎 2021-01-03 08:32

I have 1 pandas data-frame with date and time. I want to add 1 more column called session, with values morning afternoon, evening, night.

   Time
   2016-07         


        
1条回答
  •  北海茫月
    2021-01-03 09:05

    Use pd.cut to create bins for your session labels.

    df.assign(session=pd.cut(df.Time.dt.hour,[0,6,12,18,24],labels=['Night','Morning','Afternoon','Evening']))
    

    Output:

                     Time    session
    0 2016-07-10 01:18:00      Night
    1 2016-07-10 11:21:00    Morning
    2 2016-07-10 17:29:00  Afternoon
    3 2016-07-10 21:43:00    Evening
    

    0 讨论(0)
提交回复
热议问题