Reputation: 153
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-10 01:18:00
2016-07-10 11:21:00
2016-07-10 17:29:00
2016-07-10 21:43:00
I want output like
Time Session
2016-07-10 01:18:00 Night
2016-07-10 11:21:00 Morning
2016-07-10 17:29:00 Afternoon
2016-07-10 21:43:00 Evening
How to do this ?
I tried this by defining breaks
00:00 - 06:00 Night
06:00 - 12:00 Morning
12:00 - 18:00 Afternoon
18:00 -23:59 Evening
df.Time[(df.Time.dt.hour < 6) | (df.Time.dt.hour > 0) | (df.Time.dt.minute < 59) | (df.Time.dt.minute > 0)] = "Night"
But its not working properly. Thanks in advance
Upvotes: 4
Views: 2456
Reputation: 153460
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
Upvotes: 11