Reputation: 111
I have the following data frame
UNIT C/A DATETIME TOTAL COUNTs
R248 HOO7 2018-03-03 03:00:00 139.0
2018-03-03 07:00:00 927.0
2018-03-04 11:00:00 1946.0
2018-03-05 07:00:00 1330.0
2018-03-05 11:00:00 2358.0
I want to calculate the sum of 'TOTAL COUNTS' for each day.So I want the dataframe to look like this:
UNIT C/A DATETIME TOTAL COUNTs
R248 HOO7 2018-03-03 1066
2018-03-04 1946
2018-03-05 3688
I read the I have to set the index of this dataframe to 'datetime' index and then use .resample, but I am not sure how to do so.
Upvotes: 3
Views: 7142
Reputation: 863801
Use if 3 levels MultiIndex
use floor
or date
and aggregate sum
:
dates = df.index.get_level_values('DATETIME').floor('D')
Or:
dates = pd.Series(df.index.get_level_values('DATETIME').date, name='DATETIME', index=df.index)
If column DATETIME
:
dates = df['DATETIME'.dt.floor('D')
dates = df['DATETIME'].dt.date
df = df.groupby(['UNIT','C/A', dates]).sum().reset_index()
print (df)
UNIT C/A DATETIME TOTAL COUNTs
0 R248 HOO7 2018-03-03 1066.0
1 R248 HOO7 2018-03-04 1946.0
2 R248 HOO7 2018-03-05 3688.0
Solution with resample
:
df = df.set_index('DATETIME').groupby(['UNIT','C/A']).resample('D').sum().reset_index()
Or:
df = df.groupby(['UNIT','C/A', pd.Grouper(key='DATETIME', freq='D')]).sum().reset_index()
print (df)
UNIT C/A DATETIME TOTAL COUNTs
0 R248 HOO7 2018-03-03 1066.0
1 R248 HOO7 2018-03-04 1946.0
2 R248 HOO7 2018-03-05 3688.0
Upvotes: 3
Reputation: 10224
Try this:
df.groupby(df.DATETIME.dt.date)["TOTAL COUNTs"].sum()
Upvotes: 0