Pandas Rolling Mean Implementation in Python : 3 Steps Only

Pandas Rolling Mean Implementation in Python

Pandas is a great python package for manipulating data. There are many functions in it that allow you to do the manipulation. The pandas rolling mean is one of them. Using it you can find the mean of the data by taking window size n at a time and do calculations on the selected n values. In this entire tutorial, you will learn how to implement rolling mean in python with steps.

Steps to implement Pandas Rolling Mean

In this section, you will know all the steps to implement rolling mean in python. Just follow the steps for more understanding.

Step 1: Import the required package

The first step is to import all the necessary packages. In this tutorial, I am using only pandas. So let’s import it using the import statement.

import pandas as pd

Step 2: Create a Sample Dataframe

Let’s create a Sample Dataframe for implementing pandas rolling mean. I will first create a sample dataset and then apply the rolling mean over it. And then read a time-series Forex Pair dataset and find the rolling mean of the close price of it.

Simple Dataframe

import pandas as pd
data = {"col1":[10,20,30,40,50],"col2":[100,200,300,400,500]}
df = pd.DataFrame(data)
print(df)

Output

Simple Dataframe for Implementing Rolling mean
Simple Dataframe for Implementing Rolling mean

Forex Pair Dataset

import pandas as pd
df = pd.read_csv("EURUSD.csv")
print(df)

Output

Forex Pair Dataset for Implementing Rolling mean
Forex Pair Dataset for Implementing Rolling mean

Step 3: Implement the Pandas Rolling Mean Method

After creating and reading the dataset now let’s implement the rolling mean over the data. You can find the rolling mean by using the dot operator with the dataframe like your_df.rolling(window_size).mean(). Let’s find the rolling mean for the above dataset.

Simple Dataframe

import pandas as pd
data = {"col1":[10,20,30,40,50],"col2":[100,200,300,400,500]}
df = pd.DataFrame(data)
print(df.rolling(3).mean())

Output

Rolling mean for the simple dataframe
Rolling means for the simple dataframe

You can see if I apply rolling mean on dataframe directly then it will find the mean on the entire dataframe with the numerical values. Also, your are seeing NaN on values as you have taken 3 window size, that is mean will be calculated after taking the first 3 rows. So the first, second row will have NaN values.

Forex Pair Dataset

Now let’s find the rolling mean on the forex pair dataset. Here I will find the mean of the close column. To do so you have to use df[“Close”].rolling(3).mean().

Execute the below lines of code.

import pandas as pd
df = pd.read_csv("EURUSD.csv")
print(df["Close"].rolling(3).mean())

Output

Rolling mean on close price of the forex pair dataset
Rolling mean on close price of the forex pair dataset

 

Conclusion

The pandas rolling mean is very helpful in calculating the moving average of the data. The moving average is helpful for prediction. These are the example I have compiled for you on the rolling mean. I hope you have liked this tutorial. If you have any doubt then you can contact us for more help.

Source:

Dataframe Rolling Mean Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner