Data cleaning
Cleaning is a mapping from "untrustworthy" to "auditable". Never silently fix: every transformation must be visible in the delivered file.
Workflow
- Profile before touching anything.
xlsx_read/csv_readthe source. Enumerate problems explicitly (column by column): blanks, duplicates, near-duplicate labels ("N. Region" vs "North"), numbers stored as text, impossible values (negative quantities, future dates), ragged rows. - Propose the rule set in one short message when the fixes are judgment calls ("merge 'US'/'USA'/'United States' into US?" — keep going with your best rule; flag it in the log sheet).
- Produce one clean workbook with a pandas + openpyxl script run via
$DSH_OFFICE_PYTHON:Clean— tidy table: one header row, typed columns (number_formats), no merged cells inside the data area,total_rowonly when meaningful.Audit— every change applied: original value → new value → rule name → affected row references. This sheet is what makes the work trustworthy. Style per excel-analysis (banner, header fill, number formats, banded rows).
- Verify: re-read the cleaned file; assert row counts, spot-check five transformed cells against the audit entries, confirm totals match source sums where applicable.
Standard rules
- Trim whitespace; collapse internal double spaces; unify casing of categorical labels (Title Case for names, UPPER for codes).
- Dates → ISO
yyyy-mm-ddcolumn format; store real dates, never strings. - Numbers stay numbers: strip currency symbols/percent signs into the
number_formatslayer instead of baking them into strings. - Duplicates: define the key first (whole-row vs business key), keep first, record dropped keys in Audit.
- Missing values: prefer explicit placeholders the analyst can filter ("(unknown)"), documented in Audit; never invent zeros for measures.
Handoff
Report: rows in → rows kept/dropped, distinct rules applied (count each),
and where the audit lives. Offer to continue straight into analysis
(excel-analysis) on the cleaned file.