Reputation: 395
The information on df
is below:
df:
datetime value_1 value_2
2019-01-01 00:00:00 1 2
2019-01-01 00:15:00 2 2
2019-01-01 00:30:00 0 2
2019-01-01 00:45:00 4 3
2019-01-01 01:00:00 10 10
2019-01-01 01:15:00 1 2
2019-01-01 01:23:00 1 2
2019-01-01 01:45:00 1 2
2019-01-01 01:55:00 1 2
2019-01-01 02:00:00 99 99
...
And I want to sum up the value by the group and it can output the hour interval result like below:
datetime_1 datetime_2 sum_value_1 sum_value_2
2019-01-01 00:00 2019-01-01 01:00 7 9
2019-01-01 01:00 2019-01-01 02:00 14 18
...
Upvotes: 1
Views: 24
Reputation: 29742
Use pandas.Grouper
:
import pandas as pd
df['datetime'] = pd.to_datetime(df['datetime'])
new_df = df.groupby(pd.Grouper(key='datetime', freq='1h')).sum()
print(new_df)
Output
value_1 value_2
datetime
2019-01-01 00:00:00 7 9
2019-01-01 01:00:00 14 18
2019-01-01 02:00:00 99 99
Upvotes: 2