How to Calculate Rolling Mean and Sum in Pandas ?

Spread the love

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.

Syntax –

# rolling mean

# rolling sum

Examples –

Let’s read a dataset to work with.

import pandas as pd

url = ''
df = pd.read_csv(url, parse_dates=['Date'])
df = df[['Date','Close']]

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()

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()

3. Visualize the Data –

We can also visualize the data if we want.

import as px

fig = px.line(df, x='Date', y=['Close','Rolling_Mean','Rolling_Sum'])

Rating: 1 out of 5.

Leave a Reply