|
Download data_cleaning/README.md from protoneko/freelance-samples: direct link, hf CLI and curl.
- Browser
- Download file 4.63 kB
-
https://huggingface.co/datasets/protoneko/freelance-samples/resolve/main/data_cleaning/README.md
- Command line
-
hf download hf://datasets/protoneko/freelance-samples/data_cleaning/README.md
-
curl -L -o README.md https://huggingface.co/datasets/protoneko/freelance-samples/resolve/main/data_cleaning/README.md
4.63 kB
| # Data Cleaning — Messy CRM Export → Clean, Typed Dataset | |
| A reproducible end-to-end data-cleaning case study. You receive a "customer | |
| orders" export from a legacy CRM in the condition real exports arrive in: | |
| mixed date formats, duplicate rows, casing and whitespace noise, sentinel | |
| nulls, and prices that are text. This project cleans it with pandas and | |
| produces a **QA report generated from the actual run** — every fix is | |
| counted, nothing is hand-typed. | |
| The dirty data is generated by a seeded script (`make_messy.py`, seed 42), | |
| so anyone can reproduce the exact same input and verify every number in the | |
| QA report. | |
| ## The mess (real examples from `messy_orders.csv`) | |
| | Problem | Example | | |
| |---|---| | |
| | Mixed date formats | `2024-03-01`, `03/01/2024`, `01/03/2024`, `Mar 01, 2024`, `01-Mar-2024`, `2024/03/01` | | |
| | Ambiguous dates | `01/03/2024` — Jan 3 or Mar 1? | | |
| | Casing noise | `joseph thomas`, `SHIPPED`, `SAN DIEGO` | | |
| | Whitespace noise | ` san diego `, `Pending ` (trailing), double spaces | | |
| | Sentinel nulls | `N/A`, `n/a`, `NA`, `?`, empty | | |
| | Wrong types | `$1,234.56` as text, `(294.50)` accounting-negative, `4.0` where an int is expected, `ORD 10001` (space not dash) | | |
| | Duplicates | 25 exact + 15 near-duplicates (same order, different casing) | | |
| ## What the pipeline does | |
| `clean_with_pandas.py` reads everything as `dtype=str` (no silent coercion), | |
| then: | |
| 1. Strips whitespace on all cells — counted per run. | |
| 2. Converts sentinel nulls to real nulls. | |
| 3. Normalizes `order_id` (`ORD 10001` → `ORD-10001`). | |
| 4. Title-cases names/cities, lower-cases email/payment/status. | |
| 5. Parses dates against an **explicit format list** (no `guess_date`). | |
| Ambiguous `X/Y/YYYY` (both parts ≤ 12) is assumed US month-first and the | |
| count is reported — a documented, auditable decision, not a guess. | |
| 6. Casts `quantity` to int and money columns to float (`$`/`,`/`()` handled; | |
| accounting parentheses → negative, i.e. credit notes). | |
| 7. **Deduplicates last** — a whitespace-padded duplicate is only an exact | |
| duplicate *after* normalization, so order matters. Rows with a real | |
| key dedupe on `order_id`; keyless rows dedupe on the full row (never on | |
| a null key). | |
| 8. Validates: flags rows where `|total − unit_price × quantity| > 0.02`. | |
| It writes `clean.csv` and `QA_report.md` (row counts, every issue | |
| found/fixed with its rule, the date-format distribution, the ambiguity | |
| count, validation results, and a data dictionary). | |
| ## Run it | |
| ```bash | |
| python make_messy.py messy_orders.csv # reproduce the dirty input (seed 42) | |
| python clean_with_pandas.py messy_orders.csv clean.csv QA_report.md | |
| python make_previews.py # render before/after PNGs | |
| ``` | |
| ## Result of the committed run | |
| * **540 raw rows → 500 clean rows** (exactly the 40 injected duplicates removed). | |
| * **163** whitespace cells stripped; **292** casing fixes; **96** sentinel | |
| nulls; **109** money-text values; **15** `order_id` format fixes; | |
| **77** ambiguous dates resolved (assumed month-first, reported); | |
| **21** credit-note totals normalized to negative; **21** rows flagged by | |
| the total check — all of them credit notes, **no true arithmetic errors**. | |
| See `QA_report.md` for the full per-issue table. | |
| ## Before / after | |
| `before_messy.png` and `after_clean.png` show the first 13 rows of each. | |
| The contrast is the whole point: mixed-case cities, `29-Nov-2024` / `07/06/2024` / | |
| `Sep 10, 2024` / `2024-12-28` on the left become a single ISO `YYYY-MM-DD` on | |
| the right; `$1,847.64` and `(294.50)` become `1847.64` and `-294.5`. | |
| ## Why this is how I clean data in production | |
| * **Everything is counted, not assumed.** The QA report is generated from | |
| the run, so a client can audit exactly what changed and how much. | |
| * **Decisions are documented.** The month-first tie-break and the | |
| parentheses-are-negative rule are stated in the report, so a stakeholder | |
| can object to a *policy*, not discover it in a dashboard. | |
| * **Nothing is silently dropped.** Duplicates are removed by an explicit key | |
| rule; quality problems are *flagged* in counts and kept for review. | |
| * **Reproducible.** Seeded generator + `dtype=str` read means the before and | |
| after are both verifiable from scratch. | |
| ## Files | |
| * `make_messy.py` — seeded generator of the 540-row dirty input | |
| * `clean_with_pandas.py` — the cleaning + QA pipeline | |
| * `make_previews.py` — Pillow-based before/after PNG renderer | |
| * `messy_orders.csv` — the dirty input (540 rows) | |
| * `clean.csv` — the cleaned output (500 rows) | |
| * `QA_report.md` — generated QA report | |
| * `before_messy.png` / `after_clean.png` — rendered previews | |