loading…
Turn your CSV bank statement into a leak-finder.
Load a CSV bank statement, group by month + category with pandas, and plot the trends with matplotlib. Deliver a `spending.png` chart and a printed summary showing your top three leak categories.
pandas is what makes Python worth learning — and the best way to learn it is on your own data. You'll look at this chart, then change your habits.
The AI reviewer grades your submission against this exact list.
Copy this into a new file. The ... placeholders are yours to fill.
"""
Spending analyzer.
Usage:
python analyze.py statement.csv
Outputs:
spending.png — stacked bar chart of monthly category totals
Prints top 3 leak categories to stdout.
"""
from __future__ import annotations
import sys
from dataclasses import dataclass
from pathlib import Path
import pandas as pd
import matplotlib.pyplot as plt
CATEGORIES = {
"food": ["swiggy", "zomato", "restaurant", "cafe", "starbucks"],
"transport": ["uber", "ola", "petrol", "diesel", "irctc", "metro"],
"shopping": ["amazon", "flipkart", "myntra", "ajio"],
"bills": ["airtel", "jio", "electricity", "gas", "internet"],
"rent": ["rent", "landlord"],
}
@dataclass
class Summary:
top_categories: list[tuple[str, float]]
total_spend: float
def load(csv_path: Path) -> pd.DataFrame:
# TODO: read_csv, parse dates, coerce numeric amount column
...
def categorize(df: pd.DataFrame) -> pd.DataFrame:
# TODO: add 'category' column based on CATEGORIES keyword rules
# Fall back to "other" if nothing matches.
...
def aggregate(df: pd.DataFrame) -> pd.DataFrame:
# TODO: groupby([month, category]) -> sum(amount). Return a wide pivot.
...
def plot(pivot: pd.DataFrame, out: Path) -> None:
# TODO: stacked bar chart, save to out.
...
def summarize(df: pd.DataFrame) -> Summary:
# TODO: return Summary with top 3 categories + total spend
...
def main() -> None:
csv_path = Path(sys.argv[1])
df = load(csv_path)
df = categorize(df)
pivot = aggregate(df)
plot(pivot, Path("spending.png"))
s = summarize(df)
print(f"Total spend: ₹{s.total_spend:,.0f}")
for cat, amt in s.top_categories:
pct = amt / s.total_spend * 100
print(f" {cat:12s} ₹{amt:>10,.0f} ({pct:.1f}%)")
if __name__ == "__main__":
main()
pip.Push your solution to a public GitHub gist or repo (redact real transactions before committing — use a fake sample CSV). Attach the generated `spending.png` if you want the AI reviewer to critique the chart itself.