The rolling() method in Pandas helps us calculate the rolling mean or sum. The rolling mean is simply the mean of certain number of previous periods in a time series. Similarly rolling sum is the sum of certain number of previous periods in a time series.
# rolling mean df['column_name'].rolling(rolling_window).mean() # rolling sum df['column_name'].rolling(rolling_window).sum()
Let’s read a dataset to work with.
import pandas as pd url = 'https://raw.githubusercontent.com/bprasad26/lwd/master/data/ICICIBANK.NS.csv' df = pd.read_csv(url, parse_dates=['Date']) df = df[['Date','Close']] df.head()
1 . Calculate Rolling Mean in Pandas –
To calculate the rolling mean in pandas we have to write.
df['Rolling_Mean'] = df['Close'].rolling(window=5).mean() df.head(10)
Here the size of the window is 5 means for calculating the mean we will take the current row value and the previous 4 values. And that is the reason the first 4 rows has null values as there is no window of size 5. It starts with the 5th row. To calculate the mean of the 6th row we will take the value of the 6th row as well as previous 4 rows. This way all the calculation has been made for the whole dataframe. You can change the size of the window if you like. If you change it to 7 then in calculating the mean pandas will considering 7 rows of the data.
2. Calculate Rolling Sum in Pandas –
To calculate rolling sum we have to write.
df['Rolling_Sum'] = df['Close'].rolling(window=5).sum() df.head(10)
3. Visualize the Data –
We can also visualize the data if we want.
import plotly.express as px fig = px.line(df, x='Date', y=['Close','Rolling_Mean','Rolling_Sum']) fig.show()