Nik
Nik

Reputation: 43

Date Time Group By Pandas

I have count values according to date and time in my data frame. I want to group by count values according to the date time. The issue is that I have time with a interval of 15 minutes corrosponding to the vlaues. I want the values to group by per hour basis.

Input :

Date Time                  Count 
01/01/2018 12:00 AM        5
01/01/2018 12:15 AM        4 
01/01/2018 12:30 AM        9
01/01/2018 12:45 AM        12
01/01/2018 01:00 AM        2
01/01/2018 01:15 AM        5 
01/01/2018 01:30 AM        9
01/01/2018 01:45 AM        7

Output Required :

Date Time                         Count 
01/01/2018 12:00 - 01:00 AM       30
01/01/2018 01:00 - 02:00 AM       23

Please don't tell me to add the first values as this is not possible accordingly to the date

Upvotes: 2

Views: 140

Answers (1)

Allen Qin
Allen Qin

Reputation: 19947

You can use dt.floor('h'):

df.groupby(df['Date Time'].dt.floor('h')).sum()
Out[33]: 
                     Count 
Date Time                  
2018-01-01 00:00:00      30
2018-01-01 01:00:00      23

Upvotes: 3

Related Questions