Categories We Write About

Build an expenses categorizer

Here is a basic Expenses Categorizer in Python that takes a list of transactions and classifies them into predefined categories based on keywords:

python
import re # Sample categories and associated keywords CATEGORIES = { "Groceries": ["walmart", "aldi", "grocery", "supermarket", "whole foods", "costco"], "Utilities": ["electric", "water", "gas bill", "internet", "utility"], "Rent": ["rent", "landlord", "apartment"], "Transportation": ["uber", "lyft", "gas", "fuel", "bus", "metro", "train"], "Entertainment": ["netflix", "spotify", "movie", "cinema", "concert"], "Dining": ["restaurant", "cafe", "mcdonalds", "starbucks", "food", "dining"], "Healthcare": ["pharmacy", "doctor", "hospital", "medicine"], "Shopping": ["amazon", "ebay", "mall", "store", "clothing", "fashion"], "Other": [] } def categorize_expense(description): """Categorize a single expense based on description.""" description = description.lower() for category, keywords in CATEGORIES.items(): for keyword in keywords: if keyword in description: return category return "Other" def categorize_expenses(expenses): """ Categorize a list of expense transactions. Each transaction is a tuple: (description, amount) """ categorized = [] for desc, amount in expenses: category = categorize_expense(desc) categorized.append((desc, amount, category)) return categorized def print_report(categorized_expenses): """Prints a summary report of categorized expenses.""" report = {} for _, amount, category in categorized_expenses: report[category] = report.get(category, 0) + amount print("n--- Expense Report ---") for category, total in sorted(report.items(), key=lambda x: x[1], reverse=True): print(f"{category}: ${total:.2f}") # Sample usage if __name__ == "__main__": sample_expenses = [ ("Walmart Grocery", 120.50), ("Netflix Subscription", 15.99), ("Uber Ride", 23.75), ("Gas Station", 45.00), ("McDonalds Lunch", 8.99), ("Monthly Rent", 1200.00), ("Spotify Premium", 9.99), ("Electricity Bill", 60.00), ("Amazon Purchase", 130.40), ("Doctor Visit", 85.00) ] categorized = categorize_expenses(sample_expenses) print_report(categorized)

Features:

  • Matches keywords in descriptions to predefined categories.

  • Flexible for custom keyword or category updates.

  • Outputs a simple categorized summary report.

Let me know if you want a web-based or Excel-integrated version of this tool.

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About