pottolom pottolom - 10 days ago 6
Python Question

Combine Python Pandas dataframe rows with addition

I'm struggling with this. I've tried searching and have tried a few options but can't quite seem to manage it.

I want to take the following dataframe (simplified), which has a date_range as an index with 30 minute intervals:

Data
2016-11-24 00:00:00 5
2016-11-24 00:30:00 1
2016-11-24 01:00:00 4
2016-11-24 01:30:00 11
2016-11-24 02:00:00 3
2016-11-24 02:30:00 19
etc


And essentially "combine" the rows, adding the values together and changing the time series to hourly rather than 30 minute intervals, so that I end up with the following:

Data
2016-11-24 00:00:00 6
2016-11-24 01:00:00 15
2016-11-24 02:00:00 22
etc


Does anyone have any suggestions please?

Answer

You can use resample with sum:

df = df.resample('h').sum()
print (df)
                     Data
2016-11-24 00:00:00     6
2016-11-24 01:00:00    15
2016-11-24 02:00:00    22
Comments