Skip to main content

Build a Budget Tracker and Expense Analyzer

Intermediate35 min6 exercises90 XP
0/6 exercises

Knowing where your money goes is one of the most useful things you can do with data. In this project, you'll build a budget analyzer that takes a list of expenses, categorizes them, compares spending to budget limits, and generates a report with practical recommendations.

This is a great project because the same logic applies whether you're tracking personal spending, a department budget, or a company's operating costs. The scale changes, but the analysis stays the same.

Step 1: Create the Expense Data

Our dataset is a month of personal expenses. Each entry has a date, a description, an amount, and a category. We'll also define monthly budget limits for each category.

Expense data and budget limits
Loading editor...
Exercise 1: Create the Expense DataFrame
Write Code

Create an expense DataFrame from the provided dictionary and:

1. Convert the date column to datetime

2. Print the total number of expenses

3. Print the total amount spent (formatted to 2 decimal places)

4. Print the average expense amount

5. Print the most expensive single expense (description and amount)

Format:

Total expenses: 8
Total spent: $XXX.XX
Average expense: $XX.XX
Largest expense: DESCRIPTION ($XX.XX)
Loading editor...

Step 2: Categorize Expenses

Raw expense descriptions are messy. "Grocery store" and "Gas station" need to be mapped to budget categories like "Food" and "Transport." We'll build a mapping function that assigns each expense to its category.

Keyword-based expense categorization
Loading editor...
Exercise 2: Categorize Expenses
Write Code

Write a function categorize_expense(description) that maps expense descriptions to categories using keyword matching:

  • Food: grocery, coffee, restaurant, lunch
  • Utilities: electric, phone, internet
  • Transport: gas, uber
  • Entertainment: movie, concert, streaming
  • Health: gym, pharmacy
  • Other: anything that doesn't match
  • Then apply it to the DataFrame to create a category column, and print how many expenses are in each category.

    Loading editor...

    Step 3: Calculate Totals by Category

    Now that every expense has a category, we can calculate how much was spent in each category. This is the core of budget analysis: grouping expenses and summing them up.

    Exercise 3: Spending Totals by Category
    Write Code

    Write a function spending_by_category(df) that:

    1. Groups by the category column

    2. Calculates total spending, number of transactions, and average transaction amount per category

    3. Sorts by total spending (highest first)

    4. Returns the result as a DataFrame with columns: total, count, average

    Print the result.

    Loading editor...

    Step 4: Compare Spending to Budget Limits

    Having spending totals is useful, but the real insight comes from comparing them to your budget. Are you over or under in each category? By how much? This is where the analysis becomes actionable.

    Comparing spending to budget
    Loading editor...
    Exercise 4: Budget Comparison
    Write Code

    Write a function compare_to_budget(spending_totals, budget_limits) that:

  • Takes a dictionary of category spending totals and a dictionary of budget limits
  • Returns a list of dictionaries, each with keys: category, spent, budget, difference, status
  • difference is budget - spent (positive = under budget, negative = over)
  • status is "Under" if difference >= 0, else "OVER"
  • Print each category's status.

    Loading editor...

    Step 5: Identify Overspending Patterns

    Knowing you're over budget is step one. Step two is understanding why. Let's dig into the categories where spending exceeded the budget and find the specific expenses responsible.

    Exercise 5: Identify Overspending
    Write Code

    Write a function find_overspending(df, budget_limits) that:

    1. Calculates total spending per category

    2. Identifies categories that are over budget

    3. For each over-budget category, finds the largest single expense in that category

    4. Prints the overspending details

    Format for each over-budget category:

    OVER BUDGET: CATEGORY ($SPENT / $BUDGET)
      Biggest expense: DESCRIPTION ($AMOUNT)

    If no categories are over budget, print: All categories within budget!

    Loading editor...

    Step 6: Generate the Budget Report

    The final step is putting everything together into a complete budget report with recommendations. A good budget report tells you what happened, highlights the problems, and suggests what to do about them.

    Exercise 6: Complete Budget Report
    Write Code

    Write a function budget_report(df, budget_limits) that prints a complete budget report:

    1. Header: "MONTHLY BUDGET REPORT" with = separators

    2. Overview: Total spent, total budget, and overall status

    3. Category breakdown: Each category showing spent vs budget and status

    4. Recommendations: For each over-budget category, suggest reducing spending

    Use this exact format:

    ==============================
      MONTHLY BUDGET REPORT
    ==============================
    Total Spent:  $XXX.XX
    Total Budget: $XXX.XX
    Status: Under budget / OVER BUDGET
    ------------------------------
    Category Breakdown:
      Food:          $XXX.XX / $XXX.XX [STATUS]
      ...
    ------------------------------
    Recommendations:
    - Reduce CATEGORY spending by $XX.XX
    ==============================
    Loading editor...

    What You Built

    You built a complete budget tracking and analysis system:
    ---------
    Data loadingCreate and inspect expense datapd.DataFrame(), basic stats
    CategorizationMap descriptions to budget categoriesKeyword matching, .apply()
    TotalsSum spending by category.groupby().agg()
    ComparisonCheck against budget limitsDictionary lookups, conditions
    OverspendingIdentify and drill into problemsFiltering, .idxmax()
    ReportingGenerate formatted reportf-string formatting

    The keyword-based categorization and budget comparison pattern is the foundation of financial analysis tools. Professional budgeting software works on the same principles, just at a larger scale.