Reputation: 91
I have a dataframe which contains data for every 10 seconds over a period of one month. I need to calculate the moving average of the data for 15 minutes, 8 hours and 24 hours. How can I do it using Python?
Here is my dataset:
RecTime NO2_RAW NO2 Ox_RAW Ox CO_RAW CO SO2_RAW SO2
05/31/19 13:42 0 19.13 2225 4480.35 6503.1 7687.33 -3183.6 -8181.55
05/31/19 13:42 17 51.64 1711.2 3454.96 6502.7 7686.86 -3183.6 -8181.55
05/31/19 13:42 48.4 111.69 1387.7 2809.35 6501.9 7685.93 -3183.6 -8181.55
05/31/19 13:42 60.1 134.07 1173.6 2382.07 6501.4 7685.35 -3183.6 -8181.55
05/31/19 13:42 63.9 141.33 1025.6 2086.7 6501 7684.88 -3183.6 -8181.55
I have tried using the following code
> Gas_432_15min = Gas_432.resample(rule='15Min', on='RecTime').mean()
But I think it is not calculating a rolling mean.
Upvotes: 1
Views: 1626
Reputation: 150785
There's rolling
that does what you want, but require datetimeIndex
:
Gas_432.RecTime = pd.to_datetime(Gas_432.RecTime)
Gas_432.set_index('RecTime').rolling('15T').mean()
For this dataframe, you got:
+---------------------+--------+---------+---------+---------+---------+---------+---------+----------+
| 2019-05-31 13:42:00 | 0 | 19.13 | 2225 | 4480.35 | 6503.1 | 7687.33 | -3183.6 | -8181.55 |
| 2019-05-31 13:42:00 | 8.5 | 35.385 | 1968.1 | 3967.66 | 6502.9 | 7687.09 | -3183.6 | -8181.55 |
| 2019-05-31 13:42:00 | 21.8 | 60.82 | 1774.63 | 3581.55 | 6502.57 | 7686.71 | -3183.6 | -8181.55 |
| 2019-05-31 13:42:00 | 31.375 | 79.1325 | 1624.38 | 3281.68 | 6502.27 | 7686.37 | -3183.6 | -8181.55 |
| 2019-05-31 13:42:00 | 37.88 | 91.572 | 1504.62 | 3042.69 | 6502.02 | 7686.07 | -3183.6 | -8181.55 |
+---------------------+--------+---------+---------+---------+---------+---------+---------+----------+
where the first column is datetimeIndex
, the other have the same names with Gas_432
.
Upvotes: 1