+ 1
Create New Column that group values based on time period
I have a data frame that have two columns Date and Time. Date Time 2012-01-01 01:30:00 2012-01-01 2:30:12 2012-01-01 04:30:12 2012-01-01 21:30:29 2012-01-01 12:30:38 2012-01-01 09:31:05 2012-01-01 14:31:38 2012-01-01 19:31:44 2012-01-01 05:31:48 2012-01-01 07:32:23 What I want to do is to create a column Time_Category that group the time and date in to like Time1, Time2, Time3 etc. Based on if 1am - 3.59am then it is Time1. if it is 4.00am - 7.59am then Time2 like that. I am very new to python and pandas and if anyone have a idea how to do this it is very use full.
1 Answer
+ 1
Let's split your problem into small parts:
[1] read data frame to get data line by line
[2] extract 'time' in 'Time collumn' from the [1] line, then store it for later use, let's call it 'time_tmp'
[3] get the difference between 'time_tmp' and 'time_base' (could be 00:00:00 for example), classify it by checking if it meet your defined time ranges.
[4] write your result into file.
[5] repeat [2] [3] and [4] until the end of data frame
For date and time data type, you can investigate on python datetime module (FYI: timedelta object can work well with math operation like time +-/%).