Tejeshwara B.M
Tejeshwara B.M

Reputation: 13

group Timeseries by hourly count in python pandas

df['data'] = df['data'].dropna()
df['data'] = df['data'].str.strip("'(), ")
df['data'] = pd.to_datetime(df['data'], format='%Y-%m-%d %H:%M:%S.%f')
df['data'] = df['data'].dropna()

This is my data set header

0   2019-05-26 00:00:00.326000+00:00
1   2019-05-26 00:00:00.690000+00:00
2   2019-05-26 00:00:02.850000+00:00
3   2019-05-26 00:00:02.971000+00:00
4   2019-05-26 00:00:03.432000+00:00
Name: data, dtype: datetime64[ns, UTC]

I need to count hourly basis. required output


time interval : Total

0 - 1 : 5 times 1 - 2 : 10 times .. .. 23 - 24: 4 times


df[data].head()

Upvotes: 0

Views: 151

Answers (1)

Chris
Chris

Reputation: 29742

Use pandas.Series.dt.hour.

Given df:

                     data
0 2019-05-26 01:00:00.326
1 2019-05-26 02:00:00.690
2 2019-05-26 02:00:02.850
3 2019-05-26 03:00:02.971
4 2019-05-26 05:00:03.432

Using df['data'].dt.hour with pd.DataFrame.groupby.count:

import pandas as pd

df.groupby(df['data'].dt.hour).count()

Output:

      data
data      
1        1
2        2
3        1
5        1

Upvotes: 1

Related Questions