Free courses in AI, Data Science & Machine Learning Written lessons · Quizzes · Final exams
Future Data Academy
Log inGet started ↗
← Python for Data Science
FREE LESSON PREVIEW

Reliable CSV pipelines and tests

The decision

A pipeline groups sales by region. It must distinguish zero, missing cells, malformed numbers and duplicated transactions. A reliable report includes rejected-row counts and reconciliation checks rather than only a plausible total.

Synthetic case data

id,region,amount
a,North,10
b,South,0
c,North,
d,South,15
a,North,10
e,North,unknown

Work it through

Before reading the worked analysis, identify the unit of observation, required quantities and missing information. Write your own answer, including units and a limitation.

Worked analysis

Keep one copy of a. Accepted amounts are 10,0,15: North totals 10 and South 15. Record one duplicate, one missing amount and one invalid amount separately. Parse numbers after testing for blanks; “if amount” is unsafe because zero is false-like. Use a seen-ID set, totals dictionary and issues list. Conflicting duplicate IDs need a declared reconciliation policy. For money requiring exact arithmetic, consider Decimal from strings rather than binary floating point. Sort the report for stable, reproducible output.

Independent challenge

Write a csv.DictReader pipeline returning totals and a row-level issue report. Test an empty file, zero, a blank region, a negative refund, a malformed amount and a repeated ID with conflicting values. State whether refunds are legitimate and how they are reported.

Acceptance checks

Expected observed total is 25 over three accepted transactions. Sum of group totals must equal sum of accepted amounts. Keep the original input untouched.

Further reading

Official reference. This original academy exercise uses synthetic data; the reference provides background and does not endorse the academy.

Explore the full course →