How to calculate MOVING AVERAGE in Pandas DataFrame in Python

Hits: 170


How to calculate MOVING AVERAGE in Pandas DataFrame in Python

Calculating a moving average in a Pandas DataFrame in Python can be done easily using the rolling() function. This function allows you to calculate the average of a certain number of rows in a DataFrame.

First, you need to import the Pandas library and create a DataFrame. For example, you can create a DataFrame with random numbers using the numpy library.

import pandas as pd import numpy as np df = pd.DataFrame(np.random.randint(0,100,size=(100, 1)), columns=[‘data’])

Next, you can use the rolling() function to calculate the moving average. The function takes two parameters: the window size and the type of average (mean, sum, etc.). For example, to calculate the moving average of the last 3 rows, you can use the following code:

df[‘moving_average’] = df[‘data’].rolling(window=3).mean()

The window size can be changed to any number you want. For example, if you want to calculate the moving average of the last 5 rows, you can change the window size to 5.

df[‘moving_average’] = df[‘data’].rolling(window=5).mean()

You can also calculate the moving average for different types of averages, such as median or sum. To calculate the moving median, you can use the following code:

df[‘moving_median’] = df[‘data’].rolling(window=3).median()

To calculate the moving sum, you can use the following code:

df[‘moving_sum’] = df[‘data’].rolling(window=3).sum()

In this way, you can easily calculate the moving average of a Pandas DataFrame in Python using the rolling() function. You can adjust the window size and type of average to suit your needs.


In this Learn through Codes example, you will learn: How to calculate MOVING AVERAGE in Pandas DataFrame in Python.


Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners