Pandas: Calculate moving average within group

Given a pandas dataframe, we have to calculate moving average within group.
Submitted by Pranit Sharma, on November 14, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Problem statement

We are given a DataFrame containing time series for multiple objects, we need to calculate the moving average with a window size of 10 for a specific column.

Calculating moving average within group

For this purpose, we will first use the groupby() function on the required columns and apply the transform function on the object resulting from groupby and inside this transform, we will use the rolling mean method.

The groupby() method is a simple but very useful concept in pandas. By using groupby, we can create a grouping of certain values and perform some operations on those values. It splits the object, applies some operations, and then combines them to create a group hence large amounts of data and computations can be performed on these groups.

Let us understand with the help of an example,

Python program to calculate moving average within group

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating a Dictionary
d = {

# Creating DataFrame
df = pd.DataFrame(d)

# Display dataframe
print('Original DataFrame:\n',df,'\n')

# finding moving mean for window of 4
df['moving'] = df.groupby('A')['C'].transform(lambda x: x.rolling(4, 1).mean())

# Display new DataFrame
print("New DataFrame:\n",df,"\n")


The output of the above program is:

Example: Calculate moving average within group

Python Pandas Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.