The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Create a script to categorize expenses

Here’s a simple and effective Python script to categorize expenses based on predefined categories. You can run this in a local Python environment or enhance it further for integration with spreadsheets or web apps.

python
# Expense Categorization Script # Define categories and associated keywords categories = { "Groceries": ["supermarket", "grocery", "walmart", "costco", "aldi"], "Utilities": ["electric", "water", "gas", "internet", "utility"], "Transportation": ["uber", "lyft", "bus", "train", "gasoline", "fuel", "taxi"], "Entertainment": ["netflix", "movie", "cinema", "spotify", "concert", "game"], "Dining Out": ["restaurant", "dining", "cafe", "mcdonald", "starbucks", "pizza"], "Rent": ["rent", "landlord", "apartment"], "Healthcare": ["pharmacy", "doctor", "hospital", "clinic", "medicine"], "Shopping": ["amazon", "shopping", "mall", "clothes", "electronics"], "Other": [] } # Sample expense data (description, amount) expenses = [ ("Uber ride downtown", 15.50), ("Walmart grocery purchase", 82.30), ("Netflix subscription", 13.99), ("Gas station refill", 45.00), ("Starbucks coffee", 5.75), ("Rent payment", 1200.00), ("Doctor visit", 110.00), ("Amazon order", 56.49), ("Electric bill", 89.60), ("Bus pass", 25.00), ("Concert tickets", 120.00), ] # Function to categorize an expense based on its description def categorize_expense(description): description_lower = description.lower() for category, keywords in categories.items(): for keyword in keywords: if keyword in description_lower: return category return "Other" # Categorize and display each expense categorized_expenses = [] for description, amount in expenses: category = categorize_expense(description) categorized_expenses.append((description, amount, category)) # Display results print(f"{'Description':<30} {'Amount':<10} {'Category'}") print("-" * 60) for desc, amt, cat in categorized_expenses: print(f"{desc:<30} ${amt:<10.2f} {cat}") # Optional: Summarize expenses by category print("nExpense Summary by Category:") summary = {} for _, amt, cat in categorized_expenses: summary[cat] = summary.get(cat, 0) + amt for cat, total in summary.items(): print(f"{cat:<15}: ${total:.2f}")

Key Features:

  • Keyword-based classification.

  • Expense summary by category.

  • Easily extendable with more categories or input formats (e.g., CSV file, user input, etc.).

Let me know if you want this adapted for CSV import/export, GUI input, or monthly summaries.

Share this Page your favorite way: Click any app below to share.

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

We respect your email privacy

Categories We Write About