Build a Budget Tracker and Expense Analyzer
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.
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)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.
Write a function categorize_expense(description) that maps expense descriptions to categories using keyword matching:
Then apply it to the DataFrame to create a category column, and print how many expenses are in each category.
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.
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.
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.
Write a function compare_to_budget(spending_totals, budget_limits) that:
category, spent, budget, difference, statusdifference is budget - spent (positive = under budget, negative = over)status is "Under" if difference >= 0, else "OVER"Print each category's status.
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.
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!
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.
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
==============================What You Built
| You built a complete budget tracking and analysis system: | ||
|---|---|---|
| --- | --- | --- |
| Data loading | Create and inspect expense data | pd.DataFrame(), basic stats |
| Categorization | Map descriptions to budget categories | Keyword matching, .apply() |
| Totals | Sum spending by category | .groupby().agg() |
| Comparison | Check against budget limits | Dictionary lookups, conditions |
| Overspending | Identify and drill into problems | Filtering, .idxmax() |
| Reporting | Generate formatted report | f-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.