Reputation: 41
This is hourly data of 2 years and I required max and min of daily data, how can I find this?
value
record ts
2014-01-01 00:00:00 5.83
2014-01-01 01:00:00 5.38
2014-01-01 02:00:00 4.80
2014-01-01 03:00:00 3.81
2014-01-01 04:00:00 4.46
2014-01-01 05:00:00 5.04
2014-01-01 06:00:00 5.76
2014-01-01 07:00:00 6.15
2014-01-01 08:00:00 6.66
2014-01-01 09:00:00 7.02
2014-01-01 10:00:00 7.43
2014-01-01 11:00:00 7.34
2014-01-01 12:00:00 7.24
2014-01-01 13:00:00 7.71
2014-01-01 14:00:00 8.89
2014-01-01 15:00:00 10.31
Upvotes: 4
Views: 4465
Reputation: 862641
You can use resample
with Resampler.aggregate
min
and max
:
print (df)
value
record ts
2014-01-01 00:00:00 5.83
2014-01-01 01:00:00 5.38
2014-01-01 02:00:00 4.80
2014-01-01 03:00:00 3.81
2014-01-02 04:00:00 4.46
2014-01-02 05:00:00 5.04
2014-01-02 06:00:00 5.76
2014-01-03 07:00:00 6.15
2014-01-03 08:00:00 6.66
2014-01-03 09:00:00 7.02
2014-01-03 10:00:00 7.43
2014-01-04 11:00:00 7.34
2014-01-04 12:00:00 7.24
2014-01-04 13:00:00 7.71
2014-01-05 14:00:00 8.89
2014-01-05 15:00:00 10.31
#if not DatetimeIndex
df.index = pd.to_datetime(df.index)
print (df.resample('D')['value'].agg(['min', 'max']))
min max
record ts
2014-01-01 3.81 5.83
2014-01-02 4.46 5.76
2014-01-03 6.15 7.43
2014-01-04 7.24 7.71
2014-01-05 8.89 10.31
Another solution:
print (df.groupby(pd.TimeGrouper('D'))['value'].agg(['min', 'max']))
min max
record ts
2014-01-01 3.81 5.83
2014-01-02 4.46 5.76
2014-01-03 6.15 7.43
2014-01-04 7.24 7.71
2014-01-05 8.89 10.31
* piRSquared edit, thank you*
Upvotes: 4