📜  python rolling mean - Python (1)

📅  最后修改于: 2023-12-03 15:18:59.943000             🧑  作者: Mango

Python Rolling Mean

Rolling mean is a commonly used method to smooth out time series data. In Python, the pandas library provides a simple way to calculate rolling mean.

Syntax
rolling_mean = data.rolling(window=window_size).mean()

data is the time series data to be smoothed, window_size is the number of data points to include in the rolling window.

Example
import pandas as pd

# create sample time series data
data = pd.Series([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

# calculate rolling mean with window size of 3
rolling_mean = data.rolling(window=3).mean()

print(rolling_mean)

Output:

0         NaN
1         NaN
2    2.000000
3    3.000000
4    4.000000
5    5.000000
6    6.000000
7    7.000000
8    8.000000
9    9.000000
dtype: float64

As we can see, the rolling mean starts from NaN and then calculates the rolling mean for the next three data points. The process continues until the end of the time series.

Rolling Mean with Window Function

The rolling function in pandas allows for more complex window functions to be used instead of just the mean. For example, the rolling function can be used with lambda functions to apply custom functions to the rolling windows.

import pandas as pd

# create sample time series data
data = pd.Series([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

# calculate rolling mean with window size of 3
rolling_max = data.rolling(window=3).apply(lambda x: max(x))

print(rolling_max)

Output:

0     NaN
1     NaN
2     3.0
3     4.0
4     5.0
5     6.0
6     7.0
7     8.0
8     9.0
9    10.0
dtype: float64

In this example, we use a lambda function to calculate the maximum value for each rolling window of 3 data points.

Conclusion

Rolling mean is a simple yet effective method for smoothing out time series data in Python. The rolling function in pandas makes it easy to calculate rolling mean as well as other window functions.