Project: deliver the monthly report
A useful report is a small, reproducible pipeline: load → clean → calculate → group → export → chart. Keep the raw input file unchanged and save the report under a new filename.
report = clean.groupby("month", as_index=False)["amount"].sum() report = report.sort_values("month").reset_index(drop=True) report.to_csv("monthly_totals.csv", index=False)
ISO dates (YYYY-MM-DD) can be parsed with pd.to_datetime(..., errors="coerce"). Convert valid dates to a sortable month label with .dt.strftime("%Y-%m"):
clean["sold_on"] = pd.to_datetime(clean["sold_on"].str.strip(), errors="coerce") clean = clean.dropna(subset=["sold_on"]) clean["month"] = clean["sold_on"].dt.strftime("%Y-%m")
Invalid dates become missing; remove them when the report requires a known month. .dt accesses date operations on a whole column; strftime formats each date as text, with %Y for the four-digit year and %m for the two-digit month.
Do not let duplicate business keys double-count revenue or work. Clean keys before deduplicating. Missing money or time is not automatically zero. Use the explicit policy in each brief.
index=False avoids exporting an extra unnamed row-number column. Draw the chart from the same grouped table you export; a correct table paired with a misleading chart is still a bad report. Leave the learner-created figure open for capture.
See it run
import pandas as pd log = pd.DataFrame({"when": ["2025-01-05", "2025-02-03", "2025-01-20"], "amt": [30, 50, 20]}) log["when"] = pd.to_datetime(log["when"]) log["month"] = log["when"].dt.strftime("%Y-%m") report = log.groupby("month", as_index=False)["amt"].sum()
A report pipeline: parse, derive a key, then summarise.
Practice project: deliver the monthly report
Exercise 1
You own a shop's monthly sales pack. Load monthly_sales_raw.csv into clean. Strip sale and sold_on; parse sold_on as dates and amount as numeric, coercing invalid values; drop rows missing either; keep the first row per sale. Add month as YYYY-MM. Create report with columns month, amount, summed by month and sorted chronologically. Export sales_monthly.csv without a row index. Draw a bar chart as fig, ax with title "Monthly sales", x-label "Month", y-label "Sales (credits)". Leave it open.
Exercise 2
You prepare a fleet's monthly fuel-cost report. Read fuel_log_raw.csv into clean. Strip receipt and month. Convert litres and price_per_litre to numbers with coercion; remove rows missing either and keep the first row per receipt. All provided months are valid YYYY-MM labels. Add cost = litres * price_per_litre without rounding. Build report with month, cost, summed by month in chronological order; export fuel_monthly.csv without an index. Plot a line as fig, ax titled "Monthly fuel cost", x-label "Month", y-label "Cost (credits)". Leave it open.
Exercise 3
You are the project office's reporting analyst. Read project_time_raw.csv into clean. Strip entry, month, and team; convert minutes to numeric with coercion, drop missing/invalid minutes, keep the first row per entry, then add hours = minutes / 60. All months are valid YYYY-MM labels. Build report with month, team, hours, summing by month/team and sorting both labels ascending. Export team_hours_monthly.csv without an index. For the director's overview, build monthly with month, hours summed across teams and draw a bar chart as fig, ax titled "Monthly project hours", x-label "Month", y-label "Hours". Leave it open.
The explanation and tasks are public. Running this lesson requires an account with course access.
Practice
Loading Python…Visualizer Watch your code run line by line
Run your code, then choose Play to follow its execution.