Data Cleaning Exercises

Module 2 · Import correctly · 6 min

Explicit types at import

You'll be able to: Read raw files as text and convert each column deliberately afterwards.

Before this: Import everything as text first

The idea

Type inference guesses. It turns 02139 into 2139, 1/2/2026 into January 2 or February 1, and long ids into floats.

Read everything as text (pandas dtype=str, DuckDB all_varchar=true), then convert columns one by one with rules you can name.

Worked example

Input: postcode: 02139

Visual
Import → all columns as text
SQL (DuckDB)
read_csv('f.csv', all_varchar = true)
pandas
pd.read_csv("f.csv", dtype=str, keep_default_na=False)

Result: postcode stays '02139'

Common mistakes

  • Relying on inference.
  • Converting ids to numbers.

Check yourself

What does keep_default_na=False change?

Strings like NA or empty fields stay as text instead of becoming missing.

Practice it

Keep identifiers as text and parse flags.

Next: Keep identifiers as text and parse yes/no flags