Identify the Inflation Cause Using Python

Use Python to understand economic data and the cause of the current high inflation rate

Andrew Zhu
3 min readJun 23, 2022


The stock price is pluming; the crypto market is melting down; inflation is rocketing to its 40-years high; the whole economy is precarious. Everyone is talking about recession like every expert swore the inflation rate is under control just half a year ago.

Now the FED decided to control inflation. Here comes the question: What caused the inflation?

I am trying to use Python to find the answer from raw data.

Preparations for running Python code

There is no need to manually download data for analysis and plotting. Click here to get an API Key to access FRED data.

Then use the fredapi package to help fetch the series data.

pip install fredapi

For example, get SP500 data in three lines of code:

from fredapi import Fred
fred = Fred("fred api key")
sp500 = fred.get_series('SP500')

Is gas price the cause of inflation?

Read the news and watch TV, the economists and politicians claim that it is the war in European led to gas prices soaring and COVID lockdown blocked the supply chain. These two facts lead to the current high inflation rate.

But is this true? if this is true, the inflation rate spike should happen sometime after the war breaks out. Let me pull the data CPI month-over-month data from the past 10 years.

from calendar import month
from fredapi import Fred
import dateutil.parser as dp
import matplotlib.pyplot as plt
import datetime

fred = Fred("api key")

start_date = dp.parse('2012-01-01')
ukraine_war = dp.parse('2022-02-24')

cpi_mom = fred.get_series('CPIAUCSL')
cpi_mom = cpi_mom[cpi_mom.index > start_date]

,'Russia invaded Ukraine \n On 24 Feb 2022 -->'
,'Consumer Price Index \n started spike up in 2021 -->'
CPI trending in the past 10 years