I have a dataframe df which has a few weeks of 2minute resolution data:
df.dtypes
time_stamp datetime64[ns]
Day_name object
x int64
y int64
df.head
time_stamp Day_name x y
0 2017-05-17 14:28:35 Wednesday 100 200
1 2017-05-17 14:30:32 Wednesday 300 400
I want to aggregate the metrics x and y and find their average for the average '15' minute period. I originally had an epoch metric, but I've converted this to the datetime shown above.
time_stamp Day_name x y 15_min_slot
0 2017-05-17 14:28:35 Wednesday 100 200 14:15
1 2017-05-17 14:30:32 Wednesday 300 400 14:30
How do i do this?
I can find the hour via:
df['hour'] = df['time_stamp'].dt.hour
df['minute'] = df['time_stamp'].dt.minute
What I'll eventually do is then:
output = df.groupby(['15_min_slot'],as_index=False)['x'].mean()
You can use Grouper, in combination with the freq argument, i.e:
df.groupby(pd.Grouper(key=df['time_stamp'], freq='15T')).mean()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With