TT Lab
Pricing Get started
Learn Learning paths Courses

Working With Customer Data

Cleaning a Broken Orders CSV

Continue in TT Lab

Goal

You cleanse a broken CSV the customer gave you by the rules, and become able to explain with numbers what you threw away and why.

Why it matters

The most dangerous thing in a cleansing script is deleting quietly. The customer will later ask, "why are our sales only this much?", and if you cannot state the number of discarded rows and the reasons at that point, every number in that project comes under suspicion. That is why this lab has a separate step for counting the discarded rows.

One more thing. The cleansing rules are not something the engineer decides alone. You have to ask the business owner, "rows with a negative amount look like refunds — should we exclude them or count them separately?" In this lab the rules are given in advance, but remember where those rules come from in the field.

Conditions for a valid row — it must satisfy all three below.

Steps

  1. Create /root/output and save the result of cleansing /opt/data/orders.csv as /root/output/clean.csv.
  2. The first line of clean.csv must be the original header id,customer,amount,date exactly as it is.
  3. Keep only the valid rows so that clean.csv has 19 lines including the header.
  4. Check that the remaining rows satisfy all three conditions above.
  5. Write the number of discarded rows in /root/output/bad_count.txt.
  6. Write the sum of the amounts of the remaining rows after cleansing in /root/output/total.txt.
  7. Write the date with the largest sum of amounts in /root/output/top_day.txt in the form YYYY-MM-DD.
  8. Create /root/output/by_day.csv. The first line is date,total, and after that, one line per date with that day's sum of amounts.

Notes

Create the cleansed result file

Create /root/output and save the result of cleansing /opt/data/orders.csv as /root/output/clean.csv.

Create the /root/output directory and make clean.csv inside it. The original is /opt/data/orders.csv.

Preserve the header

The first line of clean.csv must be the original header id,customer,amount,date exactly as it is.

You must leave the first line of the original as it is. Only the data rows are the target of cleansing.

Keep only the valid rows

Keep only the valid rows so that clean.csv has 19 lines including the header.

Keep only the rows that satisfy all three rules. It must come to 19 lines including the header.

Check that all remaining rows satisfy the rules

Check that the remaining rows satisfy all three conditions above.

Check each of the three: the number of fields, an empty customer name, and a negative or non-numeric amount. If even one remains, it fails.

Count the discarded rows

Write the number of discarded rows in /root/output/bad_count.txt.

A cleansing that deletes quietly is a time bomb. Subtract the number of remaining rows from the number of data rows in the original.

Compute the sum of amounts

Write the sum of the amounts of the remaining rows after cleansing in /root/output/total.txt.

Add up only the amounts of the rows remaining after cleansing. If you use awk's field separator option, it is done in one line.

Find the day with the highest sales

Write the date with the largest sum of amounts in /root/output/top_day.txt in the form YYYY-MM-DD.

The criterion is the sum of amounts, not the row count. Accumulate the amounts by date and then pick the largest day.

Build the per-day aggregation table

Create /root/output/by_day.csv. The first line is date,total, and after that, one line per date with that day's sum of amounts.

Start with the date,total header, with one line per date. It reads well if you sort by date.