How to calculate the rolling average of an array in Python

Calculating a rolling average of an array returns a new array of the average of each pair of sequential numbers in the array. For example, the rolling average of [1, 2, 3] is [1.5, 2.5].

Solution for How to calculate the rolling average of an array in Python : You can use pandas.series.rolling() to calculate the rolling average of a series A rolling window object contains a window size and data from a pandas Series; it can be used for rolling operations such as the rolling average.

Call pandas.Series(data) to convert data into a pandas Series. Call series.rolling(window) with series as the pandas Series and window as 2 to return a rolling window object. Call pandas.rolling.mean() with rolling as the rolling window object from the previous step to return the rolling average of data.


how-to-calculate-the-rolling-average-of-an-array-in-python