How to calculate and plot pandas exponential moving averages?

Pandas ewm example

In today’s tutorial we will show how to calculate the exponential moving average (ewm) of a DataFrame column / Series object. Unlike the rolling moving average, the ewm provides more significance to the most recent data.

We’ll start by creating some sample interview data that we will then use in order to calculate our moving average values.

import pandas as pd
stamps = pd.date_range(start='1/1/2023', periods = 6, freq = 'Q' ).strftime('%m-%y')
interviews = [100, 133, 150, 276, 316, 209]

hr  = pd.DataFrame (dict (month = stamps, interviews = interviews))

Here is our dataset:


Find pandas column ewm with span

We use the DataFrame or Series ewm() method to calculate the moving average of one or more numeric columns in our DataFrame.

We use the span property to specify the number or periods / observations that will be taken under consideration in calculating the moving average. For example:

hr['interview_ewm_span'] = hr['interviews'].ewm(span=3).mean()

We use the alpha property to specify the smoothing average factor. For example:

hr['interview_ewm_alpha'] = hr['interviews'].ewm(alpha = 0.7).mean()

In order to visualize the difference between the different calculations, we can easily plot our DataFrame columns as a simple pandas line chart.

hr.plot(kind='line', x='month')

Here’s our plot:

How to interpret the results? For the most, the ewm calculated with alpha = 0.5 and span=3 somewhat lags behind the interviews figures. In the last month, in which we saw a significant drop in the number of interviews, the ewm overtakes the actual figures.