• author: Python and Pandas with Reuven Lerner

Analyzing US Federal Debt Using ChatGPT and Pandas

In this article, we will explore a small-scale data science project using Bamboo Weekly, pandas, and ChatGPT. Bamboo Weekly is a newsletter that covers various topics in the news and features pandas to examine public datasets. For this project, we will explore US Federal Debt using queries posed in Bamboo Weekly and executed with ChatGPT.

Background

Bamboo Weekly, hosted on Substack, takes a topic from the news and combines it with features of pandas to explore a public dataset. Recent issues have looked at grain exports from Ukraine, fuel prices across the world, egg prices in the wake of inflation, and the US Debt Ceiling. For this project, we explore the US Federal Debt and examine a data set using pandas.

Setting Up ChatGPT

To utilize ChatGPT, we will use Chachi BT, a plugin for GPT-4, a paid version of ChatGPT, costing $20/month. ChatGPT, in combination with pandas, will handle all the queries necessary for this project. For this project, we select three plugins: World News, Notable, and Zapier. Notable is the only plugin utilized in the project. It is essential to note that using GPT-4 is slower than 3.5 and limits users to 25 queries every three hours. Therefore, query structure is crucial to avoid exceeding query limits.

Utilizing Notable

To execute queries, Chachi BT uses Notable. Notable is a free service but has limits on memory and data usage. For the project, Chachi BT will download the data set and create a dataframe, followed by plotting the data using Seaborn, as requested. Notable will analyze the plot and directly provide the link to the Jupiter Notebook, where the analysis is presented.

Executing the Project

The queries used in the project are posed in Bamboo Weekly and executed with ChatGPT. The process involves creating a Jupiter notebook, downloading the data from the URL, plotting the data, and analyzing the plot.

  • Create a Jupiter notebook
  • Get data from a URL
  • Analyze the total federal debt over the last three decades
  • Use Seaborn to plot data
  • Analyze the plot in markdown

The queries served as a guide for ChatGPT, which conducted the data science process. Chachi BT utilized Notable to create and execute the code required for the project in Jupiter Notebook. The analysis performed by ChatGPT is available through a link provided by Notable.

Analyzing US Federal Debt with Python and Jupyter

In this article, we will explore how to use Python and Jupyter Notebook to analyze US federal debt data. We will be using pandas for data manipulation and Seaborn for data visualization.

Retrieving and Visualizing Data

To start our analysis, we will download data on US federal debt using the FRED API. We will use the notable library to download the data and create a pandas dataframe with the dates as the index. Here is an example of the code we will use:

import notable
import pandas as pd
import seaborn as sns

start_date = pd.Timestamp('now') - pd.offsets.Year(30)
debt_data = notable.time_series('GFDEBTN', observation_start=start_date)

sns.set_style('whitegrid')
sns.lineplot(data=debt_data, x=debt_data.index, y='value')

This code will download data on US federal debt from the last 30 years and plot it using Seaborn. We can observe the most noticeable increase in federal debt occurred in the last decade.

Interest Payments on Federal Debt

Next, we will download data on interest payments on US federal debt and plot it using Seaborn. We will use the same code structure as before but with a different data series:

import notable
import pandas as pd
import seaborn as sns

start_date = pd.Timestamp('now') - pd.offsets.Year(30)
interest_data = notable.time_series('FYOINT', observation_start=start_date)

sns.set_style('whitegrid')
sns.lineplot(data=interest_data, x=interest_data.index, y='value')

Again, we can observe the increase in interest payments on federal debt over the last 30 years.

Scatter Plot and Regression

Finally, we will create a scatter plot to compare the total federal debt and interest payments on the debt over the last 30 years. We will also include a regression line to analyze any correlation between the two variables. Here is the code we will use:

import notable
import pandas as pd
import seaborn as sns

start_date = pd.Timestamp('now') - pd.offsets.Year(30)
debt_data = notable.time_series('GFDEBTN', observation_start=start_date)
interest_data = notable.time_series('FYOINT', observation_start=start_date)

df = pd.concat([debt_data, interest_data], axis=1)
sns.regplot(data=df, x='value_x', y='value_y')

Note that we first download both data series and concatenate them into a single dataframe. Then, we use Seaborn's regplot function to create the scatter plot with regression line.

Analyzing Major Foreign Holders of US Treasury Securities

As an extra challenge, we will download and manipulate data on major foreign holders of US Treasury Securities. This data is more complex, with poorly-structured column headers and extra lines of text. Here is the code we will use to clean and visualize the data:

import notable
import pandas as pd

url = 'https://ticdata.treasury.gov/Publish/slt2d.txt'
df = pd.read_csv(url, skiprows=19, index_col=0, nrows=53, usecols=range(13), delimiter='\t')

df.columns = ['Jan 31, 2022', 'Feb 29, 2022', 'Mar 31, 2022', 'Apr 30, 2022', 'May 31, 2022',
              'June 30, 2022', 'July 31, 2022', 'Aug 31, 2022', 'Sep 30, 2022', 'Oct 31, 2022',
              'Nov 30, 2022', 'Dec 31, 2022', 'Jan 31, 2023']
df = df.drop(['Grand Total'])

for column in df.columns:
    df[column] = pd.to_numeric(df[column].str.replace(',', ''))

df = df.T

df.plot()

In this code, we first download the data from the Treasury Department's website using read_csv and setting proper arguments such as skiprows, nrows, and usecols to extract only the necessary data. Then, we rename the columns to properly represent the months and years. We also drop the "Grand Total" row and convert the values to numeric. Finally, we transpose the dataframe and plot the data.

Parsing and Visualizing Data with ChachiBT

In this article, we'll explore how to use ChachiBT, an AI chatbot, to parse and visualize data. ChachiBT is an impressive tool that can try different approaches to a problem and learn as it goes. We'll use it to parse a data file that describes how many billions of dollars are held by various countries in US debt. Then, we'll visualize the data with a pie chart using Seaborn.

Parsing the Data with ChachiBT

We started by giving ChachiBT a task to parse a data file. We asked it to create a data frame based on the data that only keeps those lines with country names in the first column, which should be used as an index. We also asked it to interpret the first line as data, where each line consists of a country name and floating-point numbers. The fields are separated by two or more space characters, and the remaining values are floating-point numbers. Additionally, we asked it to name the columns in the data frame starting with January 2023 and going down one month at a time until reaching the final column.

ChachiBT tried different approaches to parse the data, including using requests to get the raw data and splitting it into lines. It also used regular expressions to identify two or more space characters and remove the header and footer lines from the data. Despite some difficulties, ChachiBT managed to create a data frame that satisfied our requirements.

Visualizing the Data with Seaborn

After parsing the data, we asked ChachiBT to create a pie chart using Seaborn that shows the proportion of debt owned by each country in January 2023. We requested to limit the chart to display the top 15 countries and to lump all others into a single segment named "Others."

ChachiBT used Seaborn to create a pie chart that showcased the proportion of debt owned by each country in January 2023. It correctly identified the top 15 countries and grouped all others in a single segment named "Others." The pie chart provided a visual representation of the data that allowed us to quickly see the proportion of debt owned by each country.

Creating Pie Charts with Seaborn

Pie charts are a great way to visually represent proportions of data. In this article, we'll explore how to create pie charts using Seaborn. Specifically, we'll create a pie chart showing the proportion of debt owned by each country as of January 23rd, focusing on the top 15 countries. We'll also create a pie chart showing the proportion of debt owned by each continent.

Pie Chart for Top 15 Countries

Using Seaborn, we can easily create a pie chart that shows the proportion of debt owned by each country. However, because we're focusing on the top 15 countries, all others should be lumped into a single segment. This can be a bit tricky, as we want the chart to be smart enough to take the top 15 countries.

To accomplish this, we'll need to use both Seaborn and Pandas. With a little bit of programming knowledge, this can be done without too much difficulty. However, for those who are not familiar with coding, it may be necessary to seek the help of an expert to double check the results and avoid potential errors.

After some data cleaning, we were able to create a pie chart that accurately shows the proportion of debt owned by each country. From our analysis, we discovered that Japan holds the largest proportion of the debt, followed by China and the United Kingdom.

Pie Chart for Continents

To take our analysis a step further, we can create a pie chart that shows the proportion of debt owned by each continent. This requires some guesswork, as not all countries can be easily classified by continent. However, with some information at our disposal, we can make educated guesses.

We'll create a mapping of countries to continents and use this map to create a new chart. While this method may not work perfectly for every country, it still allows us to gain valuable insights from the data.

By creating a pie chart for each continent, we can see how the debt is distributed across the world. This analysis can help us better understand global debt trends and potentially identify areas where debt relief efforts may be needed.

Exploring the Power of Plug-In Chatbots

Have you ever wondered if chatbots could be intelligent enough to perform complex tasks like mapping countries to continents and classifying countries as democracies or dictatorships? The answer is yes, and that too, without requiring you to write complex code snippets. Plug-in chatbots make it possible for individuals to perform such tasks without prior knowledge in programming and data science.

In a recent article from Bamboo Weekly, the author highlighted the amazing capabilities of plug-in chatbots and their implications in the programming and data science industry. In the article, he demonstrated the power of these plug-ins by performing two intriguing tasks - mapping countries to continents and classifying countries based on their political system.

Let's dive deep into each of these tasks and explore how the author achieved them using plug-in chatbots.

Mapping Countries to Continents

Using a plug-in chatbot in the Jupiter notebook from Notable, the author was able to map a whole bunch of countries to different continents with just a few clicks. Here's a step-by-step guide on how he achieved this:

  • Used a map to create a new job.
  • Pushed the chatbot to perform the task of mapping countries to continents.
  • Created a data frame and a pie chart to visualize the mapping.

The plug-in chatbot made the task simple and convenient, but there were a few instances where it got stuck in an infinite loop. However, with some encouragement and pushing, the chatbot was able to execute the task successfully, providing accurate results.

Classifying Countries Based on Their Political System

The second task performed by the author was classifying countries based on their political systems. Though it was a more complex task, the author was still able to achieve it using plug-in chatbots in the Jupiter notebook from Notable. Here's a brief on how he achieved it:

  • Created a map of countries and their general political systems.
  • Classified countries based on their political systems and created a pie chart to showcase the proportion of debt owned by each type of government.

Though there were instances where the chatbot was reluctant to perform the task, the author succeeded in getting the chatbot to execute the task satisfactorily.

Final Thoughts

Plug-in chatbots are indeed powerful tools that can automate complex tasks with ease. While they may not be an outright replacement for programmers or data scientists, these chatbots can prove to be an asset to them.

With the growing need for automation in data science and programming, learning how to use AI systems like plug-in chatbots is becoming increasingly important. It's crucial to note that there may be instances where the chatbot might not be able to execute the task as expected, but with some nudging in the right direction, they can produce amazing results.

Overall, the author's experience with plug-in chatbots is a testament to their potential, and we can expect to see more of these blending AI and automation to make our lives easier.

Previous Post

How to Find Easy Niches for Your New AI SEO Website

Next Post

Understanding Net Promoter Score: The One Number You Need to Grow

About The auther

New Posts

Popular Post