You've probably seen the headlines: "NASA is cutting its moon mission budget by 30%." But what does that mean in real terms? How much money is actually being spent on space exploration, and what does that budget look like in the context of the entire US federal budget?
The answer lies in the NASA Budget Report, a public dataset that contains detailed line items of how the agency spends its money. But parsing and understanding this data isn't straightforward. In this article, I'll show you how to use Python to extract, analyze, and visualize NASA's budget data — and how to use this technique to answer questions about space spending that are difficult to find in official summaries.
Let's start by fetching the latest NASA budget data from the US Government's public data portal. This is a real-world example of how you can use open data to answer complex questions without needing to rely on internal sources.
import requests
import pandas as pd
from bs4 import BeautifulSoup
url = "https://www.whitehouse.gov/wp-content/uploads/2023/04/NASA-Budget-Report-2023.pdf"
response = requests.get(url)
with open("nasa_budget.pdf", "wb") as f:
f.write(response.content)
This snippet downloads the NASA budget report as a PDF file. While this isn't the most efficient way to work with PDFs, it's a good starting point. For more advanced analysis, you'd want to use a PDF parser like PyPDF2 or pdfplumber. But for now, let's focus on the data itself.
Once you have the PDF, you can use tools like Tabula or pdfplumber to extract the text and convert it into a structured format. Here's a simple example using pdfplumber:
import pdfplumber
with pdfplumber.open("nasa_budget.pdf") as pdf:
text = ""
for page in pdf.pages:
text += page.extract_text()
# Now, you can process this text to extract relevant data
This code extracts all the text from the PDF, which you can then process further. For example, you can search for keywords like "space station" or "moon" to find relevant budget lines. Once you have that data, you can analyze it using pandas or even plot it using matplotlib or seaborn.
Let's take it a step further. Suppose we're interested in finding out how much of NASA's budget is allocated to "space station" projects. Here's how you could approach that:
import re
# Example text from the PDF (for demonstration)
sample_text = """
NASA's 2023 budget includes $1.5 billion for the International Space Station (ISS),
$200 million for the Artemis program, and $300 million for space station research.
"""
# Regular expression to find budget lines related to "space station"
pattern = r"(\d+\.\d+)\s+billions? for.*space station.*"
matches = re.findall(pattern, sample_text)
budget = sum(float(m) for m in matches)
print(f"Total budget for space station projects: ${budget:,.2f} billion")
This code uses regular expressions to extract budget figures associated with "space station" projects. You can adapt this to search for other keywords like "moon," "orbital," or "deep space" to answer similar questions.
By analyzing the data this way, you can see whether NASA's budget includes funding for government-backed projects like the International Space Station or the Artemis program. This is a real-world example of how open data can be used to answer questions that are otherwise difficult to find in official summaries.
If you're looking for a tool that makes this kind of analysis easier, check out Space Budget Planner at https://intellitools.gumroad.com. It automates the process of fetching, parsing, and analyzing budget data, allowing you to focus on the insights rather than the data wrangling.
In summary, the key takeaway is that you don't need to be a data scientist to work with open data. By using Python and the right tools, you can extract valuable insights from complex datasets and answer questions that are otherwise difficult to find. Whether you're a developer, a researcher, or just a curious reader, these techniques can help you make sense of the world around you — one line of code at a time.
Top comments (0)