Editor

Identify Overspending

Intermediate · Write the code · from Build a Budget Tracker and Expense Analyzer

From the lesson: 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.

Read this part of the lesson

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!

import pandas as pd
def find_overspending(df, budget_limits):
# Your code here
pass
data = {
'description': ['Grocery store', 'Restaurant', 'Coffee', 'Electric bill',
'Movie tickets', 'Concert', 'Gas station'],
'category': ['Food', 'Food', 'Food', 'Utilities',
'Entertainment', 'Entertainment', 'Transport'],
'amount': [120.00, 85.00, 25.00, 150.00, 45.00, 95.00, 40.00]
}
df = pd.DataFrame(data)
budget = {'Food': 200.00, 'Utilities': 300.00, 'Entertainment': 100.00, 'Transport': 150.00}
find_overspending(df, budget)

More Budget Tracker exercises

All Python exercises · 50 practice problems · Practical Projects quiz