Grocery Inventory & Sales
Six stages: product master, transaction merging, stock, profits, category totals and final reports
Your project brief
Understand inventory changes and gross profit without pretending that revenue is profit.
Your task is to run each stage separately, explain the columns it creates, inspect the output CSV, and verify that the following stage consumes that exact file. A complete worked solution is provided alongside the exercises.
Six-stage processing sequence
Stage 01 — Validate product master
Check each product ID, category, price and opening stock.
input/products.csv→output/stage01.csvStage 02 — Join sales
Aggregate sales quantities by product ID, including multiple transactions per item.
output/stage01.csv→output/stage02.csvStage 03 — Calculate stock
Subtract sold quantities and refuse to permit negative remaining stock.
output/stage02.csv→output/stage03.csvStage 04 — Calculate profit
Compute revenue, cost of goods, gross profit and margin.
output/stage03.csv→output/stage04.csvStage 05 — Category performance
Add category-wise revenue and profit, rank products in each category.
output/stage04.csv→output/stage05.csvStage 06 — Publish report
Order the full product report by profit and save report.csv.
output/stage05.csv→output/report.csv
See what each stage reads and writes
Choose a stage. The table on the left shows the real source CSV; the table on the right shows the committed output CSV snapshot. Run the Python scripts locally to recreate these files.
Choose a stage to inspect the CSV data.
Input CSV
Open input CSVOutput CSV
Open output CSVCSV snapshots are read-only and are hosted with the lesson. The browser does not execute Python or edit repository data. Inspecting a file is separate from running the local pipeline.
Sample inputs and expected results
Every file is real, readable CSV. Open these files directly or use the browser's save function; no account is required.
python -m pip install pandas
cd python/pandas/projects/csv-pipelines/grocery-inventory
python run_pipeline.py
python -m unittest discover -s . -p "test_*.py"Learn from each step instead of running everything blindly. The output CSV from one stage should become the next script's input.
Student exercises and assessment
- Run all six stages one by one and explain each CSV transformation.
- Identify two new columns added between Stage 2 and Stage 4.
- Modify a copy of the first input file and predict how the final report will change.
- Create a log recording each stage's input rows, output rows and filename.
- Extend one stage to detect errors and write a
rejected.csvfile, without silently dropping incorrect records. - Challenge: Try negative quantities, unknown ProductIDs and overselling. Validate that no stock becomes negative.
Open solution guidance
Study pipeline.py for the complete implementation. Use report.csv as a final comparison and run the test suite to verify important rules.
What happens after this project?
Repeat it with your own non-sensitive sample data. For the next challenge, keep invalid rows in a separate error file, add transaction dates, schedule recurring processing and compare runs over time.
Reference lessons: Data cleaning · Sorting and GroupBy · CSV read/write · Merge/concat.