Skip to Content

Rolling Median of a Pandas Column

Rolling window statistics are very frequently used in analyzing and smoothing time-series data. In this tutorial, we will look at how to get the rolling median (over a specified rolling window) in pandas columns.

rolling median of a column

You can use the pandas rolling() function to get a rolling window over a pandas series and then apply the median() function to get the rolling median. The following is the syntax:

# s is pandas series, n is the window size
s.rolling(n).median()

Here, n is the size of the moving window you want to use, that is, the number of observations you want to use to compute the rolling statistic, in our case, the median. If you apply the above function on a pandas dataframe, it will result in a rolling median for all the numerical columns in the dataframe.

Let’s look at some examples of using the above syntax to get the rolling median. First, let’s create a sample dataframe that we will be using throughout this tutorial.

import pandas as pd

# create dataframe
df = pd.DataFrame({'PageViews': [100, 120, 180, 200, 240, 160, 130],
                   'Revenue': [10, 15, 12, 20, 30, 22, 14]},
                  index = ['2020-03-01', '2020-03-02', '2020-03-03', '2020-03-04',\
                           '2020-03-05', '2020-03-06', '2020-03-07'])
# print the dataframe
print(df)

Output:

            PageViews  Revenue
2020-03-01        100       10
2020-03-02        120       15
2020-03-03        180       12
2020-03-04        200       20
2020-03-05        240       30
2020-03-06        160       22
2020-03-07        130       14

We have a dataframe containing the daily pageviews and revenue for a blogging website.

Let’s get the 3-day rolling median of the “PageViews” columns. For this, we apply the rolling() function with a window of 3 and then apply the median() function on the pandas series.

# 3-day rolling median of PageViews
df['PageViews'].rolling(3).median()

Output:

2020-03-01      NaN
2020-03-02      NaN
2020-03-03    120.0
2020-03-04    180.0
2020-03-05    200.0
2020-03-06    200.0
2020-03-07    160.0
Name: PageViews, dtype: float64

You can see that we get NaN in the first two rows because we cannot calculate the rolling median as there are no preceding values to make the three-day window complete. The third row is 120 which is the median pageviews over the three-day window containing 100, 120, and 180.

If you apply the same function directly on a dataframe instead of individual columns, it will compute the rolling median for all the numerical columns in the dataframe. For example, let’s get the 3-day rolling median of all columns in df

# 3-day rolling median of entire dataframe df
df.rolling(3).median()

Output:

            PageViews  Revenue
2020-03-01        NaN      NaN
2020-03-02        NaN      NaN
2020-03-03      120.0     12.0
2020-03-04      180.0     15.0
2020-03-05      200.0     20.0
2020-03-06      200.0     22.0
2020-03-07      160.0     22.0

You can see that we get the 3-day median for both the columns “PageViews” and “Revenue”.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.