Data Cleaning Exercises

Exercise · beginner · 15 min

Keep identifiers as text and parse yes/no flags

Member IDs and postcodes have leading zeros that must survive, and the opt-in flag arrives as Y, yes, TRUE or 1. Parse the flag to a real boolean without guessing.

The rules

These rules decide what a correct result is. Your answer is checked on this dataset and on hidden datasets that follow the same rules.

Input
  members(member_id, postcode, opted_in) — text. member_id is unique and always present.

Rules
  member_id and postcode are identifiers: keep them as text exactly as given, except that postcode has
  ASCII whitespace removed at both ends. Never convert them to numbers (00123 must stay 00123).
  opted_in: remove ASCII whitespace at both ends, then lowercase, then:
    y, yes, true, 1   → true
    n, no, false, 0   → false
  Quarantine with reason bad_flag when opted_in is missing or anything else (a missing answer is not "no").

Outputs (row order does not matter)
  clean(member_id text, postcode text, opted_in bool)
  exceptions(member_id text, reason text)

The data

members · 17 rows, first 12 shown

member_idpostcodeopted_in
00841798506·true
000002·02139·maybe
002939missing0
015246·51389y
077927·08222N
09280330041true
085051·12134·N
00000100501yes
004197·52402true
016386·46532·no
016648·20460yes
078978missingno

What you practise

  • identifiers
  • booleans
  • mappings
  • type conversion

Your answer is also checked on hidden datasets generated from the same rules.

Download this dataset (CSV) with its data dictionary

Sign in to bookmark this and keep private notes.