To assign a column, you can create a rolling object based on your Series:

df['new_col'] = data['column'].rolling(5).mean()

The answer posted by ac2001 is not the most performant way of doing this. He is calculating a rolling mean on every column in the dataframe, then he is assigning the "ma" column using the "pop" column. The first method of the following is much more efficient:

%timeit df['ma'] = data['pop'].rolling(5).mean()
%timeit df['ma_2'] = data.rolling(5).mean()['pop']

1000 loops, best of 3: 497 µs per loop
100 loops, best of 3: 2.6 ms per loop

I would not recommend using the second method unless you need to store computed rolling means on all other columns.

Answer from Andrew L on Stack Overflow
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.rolling.html
pandas.DataFrame.rolling — pandas 3.0.6 documentation
For a DataFrame, a column label or Index level on which to calculate the rolling window, rather than the DataFrame’s index.
🌐
Calmcode
calmcode.io › course › pandas-datetime › rolling-stats
Calmcode - pandas datetime: Rolling Stats
To calculate a rolling mean, you can call .rolling() on the dataframe. This returns an object that represents rolling subsets of the entire dataframe.
Top answer
1 of 3
99

To assign a column, you can create a rolling object based on your Series:

df['new_col'] = data['column'].rolling(5).mean()

The answer posted by ac2001 is not the most performant way of doing this. He is calculating a rolling mean on every column in the dataframe, then he is assigning the "ma" column using the "pop" column. The first method of the following is much more efficient:

%timeit df['ma'] = data['pop'].rolling(5).mean()
%timeit df['ma_2'] = data.rolling(5).mean()['pop']

1000 loops, best of 3: 497 µs per loop
100 loops, best of 3: 2.6 ms per loop

I would not recommend using the second method unless you need to store computed rolling means on all other columns.

2 of 3
14

Edit: pd.rolling_mean is deprecated in pandas and will be removed in future. Instead: Using pd.rolling you can do:

df['MA'] = df['pop'].rolling(window=5,center=False).mean()

for a dataframe df:

          Date    stock  pop
0   2016-01-04  325.316   82
1   2016-01-11  320.036   83
2   2016-01-18  299.169   79
3   2016-01-25  296.579   84
4   2016-02-01  295.334   82
5   2016-02-08  309.777   81
6   2016-02-15  317.397   75
7   2016-02-22  328.005   80
8   2016-02-29  315.504   81
9   2016-03-07  328.802   81

To get:

          Date    stock  pop    MA
0   2016-01-04  325.316   82   NaN
1   2016-01-11  320.036   83   NaN
2   2016-01-18  299.169   79   NaN
3   2016-01-25  296.579   84   NaN
4   2016-02-01  295.334   82  82.0
5   2016-02-08  309.777   81  81.8
6   2016-02-15  317.397   75  80.2
7   2016-02-22  328.005   80  80.4
8   2016-02-29  315.504   81  79.8
9   2016-03-07  328.802   81  79.6

Documentation: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rolling.html

Old: Although it is deprecated you can use:

df['MA']=pd.rolling_mean(df['pop'], window=5)

to get:

          Date    stock  pop    MA
0   2016-01-04  325.316   82   NaN
1   2016-01-11  320.036   83   NaN
2   2016-01-18  299.169   79   NaN
3   2016-01-25  296.579   84   NaN
4   2016-02-01  295.334   82  82.0
5   2016-02-08  309.777   81  81.8
6   2016-02-15  317.397   75  80.2
7   2016-02-22  328.005   80  80.4
8   2016-02-29  315.504   81  79.8
9   2016-03-07  328.802   81  79.6

Documentation: http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.rolling_mean.html

🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas rolling() mean, average, sum examples
Pandas rolling() Mean, Average, Sum Examples - Spark By {Examples}
October 14, 2024 - pandas.DataFrame.rolling() function can be used to get the rolling mean, average, sum, median, max, min e.t.c for one or multiple columns. Rolling mean is
🌐
Statology
statology.org › home › how to calculate a rolling mean in pandas
How to Calculate a Rolling Mean in Pandas
October 16, 2023 - A rolling mean is simply the mean of a certain number of previous periods in a time series.
🌐
Medium
medium.com › @whyamit101 › understanding-pandas-rolling-f8f6d6796c07
Understanding Pandas Rolling. If you think you need to spend $2,000… | by why amit | Medium
February 26, 2025 - You might have heard the term “rolling” tossed around in the context of data analysis, but what does it actually mean? In Pandas, the rolling() function allows you to perform window-based calculations on your data.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pandas-dataframe-rolling
Python | Pandas dataframe.rolling() - GeeksforGeeks
February 21, 2022 - Pandas dataframe.rolling() function provides the feature of rolling window calculations. The concept of rolling window calculation is most primarily used in signal processing and time-series data.
Find elsewhere
🌐
datagy
datagy.io › home › pandas tutorials › data analysis in pandas › how to calculate a rolling average (mean) in pandas
How to Calculate a Rolling Average (Mean) in Pandas • datagy
April 2, 2023 - We then apply the rolling average ... a minimum of 3 period for a valid result. We use the mean() function to calculate the actual rolling average for each window within the groups....
🌐
EDUCBA
educba.com › home › software development › software development tutorials › pandas tutorial › pandas rolling
Pandas rolling | How rolling() Function works in Pandas Dataframe?
June 7, 2023 - Pandas rolling() function gives the element of moving window counts. The idea of moving window figuring is most essentially utilized in signal handling and time arrangement information.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Programiz
programiz.com › python-programming › pandas › methods › rolling
Pandas rolling()
In this example, the rolling() method calculates the mean using a specified window size. We've set window=2, which means it calculates the mean of every 2 consecutive values. The parameter min_periods is set to 2, which means that at least 2 non-NaN values are needed to compute the mean.
🌐
GeeksforGeeks
geeksforgeeks.org › python › pandas-rolling-mean-by-time-interval
Pandas - Rolling mean by time interval - GeeksforGeeks
July 23, 2025 - The First 29 rows of the column MA30 will have a value NULL and the first non NULL value will be at row 30. Now we will be calculating the rolling mean with a window of 200.
Top answer
1 of 9
129

In the meantime, a time-window capability was added. See this link.

In [1]: df = DataFrame({'B': range(5)})

In [2]: df.index = [Timestamp('20130101 09:00:00'),
   ...:             Timestamp('20130101 09:00:02'),
   ...:             Timestamp('20130101 09:00:03'),
   ...:             Timestamp('20130101 09:00:05'),
   ...:             Timestamp('20130101 09:00:06')]

In [3]: df
Out[3]: 
                     B
2013-01-01 09:00:00  0
2013-01-01 09:00:02  1
2013-01-01 09:00:03  2
2013-01-01 09:00:05  3
2013-01-01 09:00:06  4

In [4]: df.rolling(2, min_periods=1).sum()
Out[4]: 
                       B
2013-01-01 09:00:00  0.0
2013-01-01 09:00:02  1.0
2013-01-01 09:00:03  3.0
2013-01-01 09:00:05  5.0
2013-01-01 09:00:06  7.0

In [5]: df.rolling('2s', min_periods=1).sum()
Out[5]: 
                       B
2013-01-01 09:00:00  0.0
2013-01-01 09:00:02  1.0
2013-01-01 09:00:03  3.0
2013-01-01 09:00:05  3.0
2013-01-01 09:00:06  7.0
2 of 9
54

What about something like this:

First resample the data frame into 1D intervals. This takes the mean of the values for all duplicate days. Use the fill_method option to fill in missing date values. Next, pass the resampled frame into pd.rolling_mean with a window of 3 and min_periods=1 :

pd.rolling_mean(df.resample("1D", fill_method="ffill"), window=3, min_periods=1)

            favorable  unfavorable     other
enddate
2012-10-25   0.495000     0.485000  0.025000
2012-10-26   0.527500     0.442500  0.032500
2012-10-27   0.521667     0.451667  0.028333
2012-10-28   0.515833     0.450000  0.035833
2012-10-29   0.488333     0.476667  0.038333
2012-10-30   0.495000     0.470000  0.038333
2012-10-31   0.512500     0.460000  0.029167
2012-11-01   0.516667     0.456667  0.026667
2012-11-02   0.503333     0.463333  0.033333
2012-11-03   0.490000     0.463333  0.046667
2012-11-04   0.494000     0.456000  0.043333
2012-11-05   0.500667     0.452667  0.036667
2012-11-06   0.507333     0.456000  0.023333
2012-11-07   0.510000     0.443333  0.013333

UPDATE: As Ben points out in the comments, with pandas 0.18.0 the syntax has changed. With the new syntax this would be:

df.resample("1d").sum().fillna(0).rolling(window=3, min_periods=1).mean()
🌐
Finxter
blog.finxter.com › home › learn python blog › how to use pandas rolling – a simple illustrated guide
How to Use Pandas Rolling - A Simple Illustrated Guide - Be on the Right Side of Change
April 11, 2022 - What does the pandas.DataFrame.rolling() method do? In short, it performs rolling windows calculations. It is often used when working with time-series data or signal processing. I will shortly dive into a few practical examples to clarify what ...
🌐
Pandas
pandas.pydata.org › docs › dev › reference › window.html
Rolling window functions - Pandas - PyData |
pandas.api.typing.Rolling instances are returned by .rolling calls: pandas.DataFrame.rolling() and pandas.Series.rolling(). pandas.api.typing.Expanding instances are returned by .expanding calls: pandas.DataFrame.expanding() and pandas.Series.expanding().
🌐
Educative
educative.io › answers › how-to-compute-the-rolling-mean-of-a-time-series-in-python
How to compute the rolling mean of a time series in Python
Line 4: We load the time series data into a DataFrame using the read_csv() function from pandas. Line 7: We use the rolling() function to compute the rolling mean with a window size of 3.
🌐
HAR Data Extractor
jonathansoma.com › lede › foundations-2018 › pandas › rolling-averages-in-pandas
Rolling averages in pandas
import pandas as pd import matplotlib.pyplot as plt %matplotlib inline ... You can use df.rolling, and then ask it for the .mean().