Data Cleaning
Beginner 12 min read Data Cleaning

Standardizing Text and Strings

Same thing, spelled a thousand ways — until you normalize

Names, addresses, IDs and statuses arrive in a dozen costumes. Case folding, whitespace stripping, canonical value mapping and regex parsing turn messy strings into join-ready keys.

stringsnormalizecasestripregexcanonicalmapping
Prerequisites: Handling Duplicates
Before we start

What you will learn

  • Normalize case, whitespace and unicode
  • Map variants to canonical values
  • Parse structured text with regex
Introduction

What is this?

Text cleaning is the quiet enabler of every join and every category: lowercase everything, strip whitespace, expand abbreviations, map 'USA'/'U.S.A'/'us' to one canonical token, and use regex to pull structure — dates, emails, phone numbers — out of free text. Standardization turns fuzzy categories into hard keys.

Motivation

Why do you need it?

  • Sloppy case breaks joins and makes 'austin' and 'Austin' different cities.
  • Headers and statuses with trailing spaces silently create new categories.
  • Parsing structured text (email, SSN, SKU) unlocks fields that otherwise defy aggregation.
  • Canonical mappings let 'monthly plan', 'm', 'MTHLY' collapse to one label.
Think about it

Mental model

Imagine

A mail sorter with a name-and-address normalization table

Think of

Text standardization

The sorter converts 'MR John A SMITH Jr', 'John Smith', and 'J. Smith' into one canonical delivery key before routing; otherwise the same household 'splits' into three addresses. Same principle: one canonical spelling per entity.

Mechanics

How it works

  • str.lower()/.upper() fold case; .strip()/replace remove surrounding and repeated whitespace.
  • str.replace and .map(dict) collapse known variants to canonical tokens.
  • Regex (str.extract/str.contains/str.replace with patterns) parses structured pieces.
  • Unicode normalization (unicodedata.normalize) fixes accent-doubled names.
  • Canonical maps live as dicts/config so the rules are auditable.
  • Verify with value_counts() before and after to see categories collapse.

Messy tokens → canonical keys.

Raw text
Fold
Map
Parse
Key-ready
Hands-on

Examples

Collapse variants into one city

Collapse variants into one city
import pandas as pd
df = pd.DataFrame({"city": ["Austin", "austin ", "AUSTIN", "Houston", " houston", "HOUSTON TX"]})
canon = {"austin": "austin", "houston": "houston", "houston tx": "houston"}
df["city_key"] = df["city"].str.strip().str.lower().map(canon)
print(df)
print(df["city_key"].value_counts())

Strip and lowercase first, then a canonical map collapses every spelling into one key per city.

Real world

In production

  • A support analytics pipeline maps case-variant status strings into three canonical states.
  • An address service normalizes street abbreviations so records join on one address key.
  • A SKU ingestion parses product codes with regex to extract brand and size fields.
Guidelines

Best practices

  • Standardize on the smallest trusted key: lowercase + strip as the default move.
  • Keep the variant→canonical dict visible; it's the business logic.
  • Do text fixes before dedupe and join — keys must agree first.
  • Check value_counts() after every transform to see collapse and leaks.
Watch out

Common mistakes

  • Lowercasing data you must display verbatim elsewhere — keep a raw column.
  • Using regex on data that only needed strip/lower (overkill that adds bugs).
  • Mapping after trimming is fine; trimming after mapping is not — strips rarely a canonical spelling.
  • Assuming trailing spaces don't exist — they always do in exports.
When things break

Debugging tips

  • Categories not collapsing → inspect repr of value_counts indexes for invisible spaces.
  • Regex match nothing → test the pattern on 10 raw examples first, then the column.
  • A mapped column full of NaN means variant keys missed the dict — print unique() before mapping.
  • Unicode accents: normalize before lower or 'café' and 'cafe' split.
Interviews

Interview checkpoint

Practice

Hands-on lab

Normalize Variant Address Data
30 min · beginner
In progress
Objectives
  • Strip, fold, and collapse address variants
  • Parse a zip code with regex
  • Verify no variants survive normalization

Normalize Variant Address Data

30 min · beginner

0/4 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/2
1

Which best turns ' NEW YORK ' into 'new york'?

2

Regex is the right first tool for most whitespace problems.

Go deeper

Official documentation