Pandas – Rolling mean by time interval
In this article, we will be looking at how to calculate the rolling mean of a dataframe by time interval using Pandas in Python.
Pandas dataframe.rolling() is a function that helps us to make calculations on a rolling window. In other words, we take a window of a fixed size and perform some mathematical calculations on it.
Syntax: DataFrame.rolling(window, min_periods=None, center=False, win_type=None, on=None, axis=0).mean()
- window : Size of the window. That is how many observations we have to take for the calculation of each window.
- min_periods : Least number of observations in a window required to have a value (otherwise result is NA).
- center : It is used to set the labels at the center of the window.
- win_type : It is used to set the window type.
- on : Datetime column of our dataframe on which we have to calculate rolling mean.
- axis : integer or string, default 0
Dataset Used: Tesla_Stock
Step 1: Importing Libraries
Step 2: Importing Data
We will be calculating the rolling mean of the column ‘Close’ of the DataFrame.
Step 3: Calculating Rolling Mean
The First 29 rows of the column MA30 will have a value NULL and the first non NULL value will be at row 30. Now we will be calculating the rolling mean with a window of 200.
For ‘MA200’ the first non-NULL will be at row 200. Now lets plot ‘MA30’ , ‘MA200’ and ‘Close’ for better visualization
Step 4: Plotting
Please Login to comment...