Issue
I have a timestamp_time
column in a DataFrame (in string format)
timestamp_time
11:59 AM
5:06 AM
11:04 PM
5:06 PM
....
....
12:00 AM
12:01 PM
I want to bin the values in these range (inclusive)
morning -> 5AM to 11:59AM
afternoon ->12PM to 4:59PM
evening -> 5PM to 8:59PM
night -> 9PM to 11:59PM
midnight -> 12AM to 4:59AM
I want to generate a new time_of_the_day
column based on the value (and the mentioned range) from timestamp_time
column
The Output will be like this
time_of_the_day
morning
morning
night
evening
....
....
midnight
afternoon
I converted the timestamp_time
to 24-hour time format
What to do next?
Solution
You will need a function to transform a certain time to the time range(time_of_the_day)
def time_range(time):
hour = datetime.strptime(time, '%I:%M %p').hour
if hour > 20:
return "Night"
elif hour > 16:
return "Evening"
elif hour > 11:
return "Afternoon"
elif hour > 4:
return "Morning"
else:
return "Midnight"
then use apply method using the function we created to transform you data, and set it your new column(feature)
df["time_of_the_day"] = df.timestamp_time.apply(lambda time: time_range(time))
Answered By - Yanni TheDeveloper
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.