Where did my salary go? Track spending with Python
Your bank app lists payments one by one. What you actually want to know is where the money went: how much on food, how much on travel, and whether you stayed under budget.
This program takes a month of payments, adds them up per category, draws a bar chart in plain text and compares the total with your budget. Type your own budget below and press Run.
Turn a month of UPI payments into a spending chart, and spot the leak.
The Python behind it · 28 lines
1budget = 30000 # your monthly budget in ₹23# One month of UPI payments (sample data, in your own app you'd load the bank CSV)4payments = [5 ("Swiggy", "Food", 420), ("Zomato", "Food", 610), ("Uber", "Travel", 280),6 ("Rent", "Home", 12000), ("Blinkit", "Groceries", 1890), ("Netflix", "Fun", 649),7 ("Swiggy", "Food", 380), ("Metro card", "Travel", 500), ("Amazon", "Shopping", 2399),8 ("Zomato", "Food", 545), ("Electricity", "Home", 1430), ("Movie", "Fun", 900),9 ("Blinkit", "Groceries", 1240), ("Ola", "Travel", 340), ("Myntra", "Shopping", 1799),10]1112totals = {}13for shop, category, amount in payments:14 totals[category] = totals.get(category, 0) + amount1516spent = sum(totals.values())17for category, amount in sorted(totals.items(), key=lambda kv: -kv[1]):18 bar = "█" * round(amount / spent * 40)19 print(f"{category:<10} ₹{amount:>6,} {bar}")2021print(f"\nSpent ₹{spent:,} of ₹{budget:,}")22if spent > budget:23 extras = {c: a for c, a in totals.items() if c != "Home"}24 print(f"⚠ Over budget by ₹{spent - budget:,}. Biggest spend after rent: {max(extras, key=extras.get)}")25else:26 print(f"✓ You saved ₹{budget - spent:,} this month")27food = [a for s, c, a in payments if c == "Food"]28print(f"Food delivery: {len(food)} orders, ₹{sum(food):,} — about ₹{sum(food) * 12:,} a year")
How the code works, step by step
1.Store each payment as a tuple
Every payment is (shop, category, amount). A list of tuples is the simplest way to hold rows of data. In a real app you would read these rows from your bank's CSV statement instead of typing them.
4payments = [5 ("Swiggy", "Food", 420), ("Zomato", "Food", 610), ("Uber", "Travel", 280),6 ("Rent", "Home", 12000), ("Blinkit", "Groceries", 1890), ("Netflix", "Fun", 649),7 ("Swiggy", "Food", 380), ("Metro card", "Travel", 500), ("Amazon", "Shopping", 2399),8 ("Zomato", "Food", 545), ("Electricity", "Home", 1430), ("Movie", "Fun", 900),9 ("Blinkit", "Groceries", 1240), ("Ola", "Travel", 340), ("Myntra", "Shopping", 1799),10]2.Add up each category with a dictionary
The dictionary maps a category to its running total. totals.get(category, 0) returns 0 the first time a category appears, so you never need a separate check.
12totals = {}13for shop, category, amount in payments:14 totals[category] = totals.get(category, 0) + amount3.Draw the bar chart
Sorting by the negative amount puts the biggest category first. Each bar is the █ character repeated in proportion to that category's share of the total, and the f-string pads the columns so they line up.
16spent = sum(totals.values())17for category, amount in sorted(totals.items(), key=lambda kv: -kv[1]):18 bar = "█" * round(amount / spent * 40)19 print(f"{category:<10} ₹{amount:>6,} {bar}")4.Compare with your budget
An if/else decides the message. A list comprehension pulls out just the food orders, and multiplying by 12 turns one month into a yearly figure, which is usually the number that makes people change a habit.
21print(f"\nSpent ₹{spent:,} of ₹{budget:,}")22if spent > budget:23 extras = {c: a for c, a in totals.items() if c != "Home"}24 print(f"⚠ Over budget by ₹{spent - budget:,}. Biggest spend after rent: {max(extras, key=extras.get)}")25else:26 print(f"✓ You saved ₹{budget - spent:,} this month")27food = [a for s, c, a in payments if c == "Food"]28print(f"Food delivery: {len(food)} orders, ₹{sum(food):,} — about ₹{sum(food) * 12:,} a year")
Make it yours
- Download your bank statement as CSV and replace the payments list with the csv module.
- Add a category limit, e.g. warn when Food goes over ₹3,000.
- Run it every month and keep the totals in a file to see the trend.
Questions
- Do I need pandas for this?
- No. This version uses only built-in Python: a list, a dictionary and a loop. pandas helps once you have thousands of rows or several months, and the same idea becomes a one-line groupby.
- Is the payment data real?
- It is sample data typed into the program. Replace it with your own statement to get your real numbers; nothing leaves your browser when you run it here.