Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jupyter-naas
GitHub Repository: jupyter-naas/awesome-notebooks
Path: blob/master/FED/FED_Visualize_Inflation_Rate.ipynb
2973 views
Kernel: Python 3

FED.png

FED - Visualize Inflation Rate

Give Feedback | Bug report

Tags: #fed #inflation_rate #vizualization #plotly

Author: Mohit Singh

Last update: 2023-04-12 (Created: 2023-04-06)

Description: This notebook vizualize the inflation rate of the US using plotly and fred api

Input

import naas import pandas as pd import plotly.express as px # fredapi try: from fredapi import Fred except ModuleNotFoundError: !pip install fredapi from fredapi import Fred

Setup Variables

  • fred_api_key: Fred API Key, to obtain an Fred API key, please refer to the St. Louis Fed Website.

  • series_id: A series ID is used to retrieve data for a specific economic indicator from the FRED database, please refer to the FRED Documentation

## api key fred_api_key = naas.secret.get("FRED_API_KEY")
## series id series_id = 'CPALTT01USM657N'
## initialize Fred with api key fred = Fred(api_key=fred_api_key)
# Define the start date for the inflation rate data start_date = "1960-01-01"
# Retrieve the inflation rate data data = fred.get_series(series_id, start_date)

Model

# Convert the data to a Pandas DataFrame df = pd.DataFrame(data, columns=["inflation rate"])
# Resample the data to the annual frequency and calculate the mean df = df.resample("A").mean()
# Create a line plot using Plotly fig = px.line(df, x=df.index, y="inflation rate", title="US Inflation Rate (1960-2023)")

Output

## display the plot fig.show()