DEV Community

Adam Cooper
Adam Cooper

Posted on

Code Your Budget: A 20-Minute Python Script That Categorizes Spending from CSV

#ai

Budgets are just data pipelines in disguise.

If you’ve ever tracked expenses manually or relied on budgeting apps that feel bloated or restrictive, it’s time to take control.

In this guide, you’ll build a simple Python script that automatically categorizes spending from a CSV file — no subscriptions, no dashboards, just clean automation.

By the end, you’ll have a fully functional money management tool that runs faster than any spreadsheet.


Why Automate Your Budget?

For developers, automation isn’t about saving effort — it’s about improving accuracy and freeing up mental bandwidth.

Manual budgeting is slow and prone to bias. You forget small purchases, mislabel expenses, or skip reconciliation entirely.

A Python-based system fixes that. Once set up, it:

  • Reads transactions directly from your CSV (bank exports, for instance).
  • Categorizes them based on keywords or rules.
  • Summarizes spending by category, month, or merchant.

It’s simple, auditable, and scalable — just like good code.


Step 1: Prepare Your Data

Export your latest transactions from your banking app or credit card account. Most will come as a CSV with columns like:

Date, Description, Amount
2025-01-04, AMAZON MARKETPLACE, -45.32
2025-01-05, UBER TRIP, -13.60
2025-01-06, SALARY PAYMENT, 2500.00

Enter fullscreen mode Exit fullscreen mode

Save it as transactions.csv in your project directory.


Step 2: Define Your Categories

Start by creating a dictionary of keywords mapped to categories.

categories = {
    "AMAZON": "Shopping",
    "UBER": "Transport",
    "STARBUCKS": "Food & Drink",
    "SALARY": "Income",
    "NETFLIX": "Entertainment"
}

Enter fullscreen mode Exit fullscreen mode

This is your rule engine. You can expand or fine-tune it as your spending patterns evolve.


Step 3: Write the Categorization Script

Now, let’s build the core logic.

import csv

def categorize_transaction(description):
    for keyword, category in categories.items():
        if keyword in description.upper():
            return category
    return "Uncategorized"

with open("transactions.csv", "r") as infile, open("categorized.csv", "w", newline="") as outfile:
    reader = csv.DictReader(infile)
    fieldnames = reader.fieldnames + ["Category"]
    writer = csv.DictWriter(outfile, fieldnames=fieldnames)
    writer.writeheader()

    for row in reader:
        row["Category"] = categorize_transaction(row["Description"])
        writer.writerow(row)

Enter fullscreen mode Exit fullscreen mode

Run the script, and you’ll generate a new file called categorized.csv — complete with an added “Category” column.


Step 4: Summarize Spending by Category

Now that your data is organized, you can add a quick spending summary.

from collections import defaultdict

totals = defaultdict(float)

with open("categorized.csv", "r") as f:
    reader = csv.DictReader(f)
    for row in reader:
        amount = float(row["Amount"])
        totals[row["Category"]] += amount

for category, total in totals.items():
    print(f"{category}: ${total:.2f}")

Enter fullscreen mode Exit fullscreen mode

You’ll now see an output like:

Shopping: -$245.80
Food & Drink: -$78.60
Transport: -$32.40
Income: $2500.00

Enter fullscreen mode Exit fullscreen mode

In just a few lines, you’ve built a fully automated budgeting engine.


Step 5: Expand and Optimize

Once you have the basics running, there are easy ways to level it up:

  • Add monthly summaries using Python’s datetime library.
  • Use pandas for faster data analysis and visualization.
  • Integrate with a dashboard tool like Streamlit for a simple UI.
  • Connect to APIs for real-time financial data pulls.

This is the same kind of logic Finelo uses behind its learning tools — turning complex financial workflows into small, automated systems.


Why Finelo Teaches Developers to Automate Finances

At Finelo, we believe financial literacy should feel like writing good code — clean, consistent, and empowering.

Our AI-guided lessons on budgeting, investing, and automation teach you to build systems that grow with you, not apps that box you in.

Automation is the bridge between financial theory and daily practice.

When you automate your budget, you stop managing money reactively and start engineering your financial future.


Conclusion: Code Meets Cash Flow

You don’t need expensive tools to understand where your money goes — you just need a simple script and a clear system.

In 20 minutes, you’ve built a transparent, customizable budgeting framework that gives you data-driven insight into your finances.

To learn more about automating your personal finance systems, explore Finelo’s practical AI learning modules** at Finelo.com — where financial literacy meets engineering logic.

Top comments (0)