Data Science ยท Chapter 5 of 43

Data Cleaning

Cleaning fixes MISSING VALUES, DUPLICATES, WRONG TYPES, OUTLIERS and INCONSISTENT LABELS. It's the largest and least glamorous chunk of any project.

Document every cleaning decision โ€” future-you will need it.

Example 1 (python)
import pandas as pd
df = pd.read_csv('data.csv')
df = df.drop_duplicates()
df['age'] = df['age'].fillna(df['age'].median())

Drop duplicates, fill NaN with median.

Example 2 (python)
df['email'] = df['email'].str.strip().str.lower()

Standardise text fields.

Key points

  • Handle missing values.
  • Remove duplicates.
  • Fix types and text formatting.
  • Log every cleaning decision.
๐Ÿ’ก Note: Never delete rows silently โ€” always print how many rows were dropped and why.

๐Ÿ“ Quick Quiz

1. drop_duplicates removes:

2. A good default for numeric NaNs is:

3. Cleaning should be: