US Debt Data Analysis
This project analyzes public debt data from the U.S. Treasury’s official API (fiscaldata.treasury.gov). The script fetches real-time “Debt Subject to Limit” data, processes the JSON response, and loads it into a pandas DataFrame for analysis.
A key analysis step involves grouping the data by debt category (e.g., ‘Public Debt’, ‘Intragovernmental Holdings’) using df.groupby(). The size of these categories is then visualized using .plot(kind='barh') to provide a clear, horizontal bar chart comparing the volume of records for each debt type.
# Public API
# https://github.com/public-apis/public-apis
import requests
## test api
url = "https://api.fiscaldata.treasury.gov/services/api/fiscal_service/v1/accounting/dts/debt_subject_to_limit"
resp = requests.get(url)
resp.status_code
## Check Status code first
if resp.status_code == 200:
print("ok")
else:
print("Please check the path again")
## Retrieve data from APO( Fetch content)
resp.json()
# import data
import pandas as pd
if resp.status_code == 200:
data = resp.json()
df = pd.DataFrame(data['data'])
else:
print(f"Please check the path again. HTTP status code: {resp.status_code}")
#groups df by category, counts the items in each group, and then plots those counts as a horizontal bar chart.
df.groupby('debt_catg').size().plot(kind='barh'


Leave a Reply