Free preview: Cleaning messy data in pandas

Free preview lesson from Python for Data.

Real-world data is rarely analysis-ready. A short, repeatable cleaning routine saves time and prevents wrong conclusions.

Common fixes

# Remove exact duplicate rows
df = df.drop_duplicates()

# Convert text to numbers, turning bad values into NaN
df['amount'] = pd.to_numeric(df['amount'].str.replace(',', ''), errors='coerce')

# Parse dates
df['date'] = pd.to_datetime(df['date'], errors='coerce')

# Standardize text
df['region'] = df['region'].str.strip().str.title()

Missing values

Decide per column: drop the rows, fill with a sensible value, or leave them and handle them in analysis. Write down which you chose and why — it affects results.

Check your work

After cleaning, rerun df.info() and df.isna().sum() and compare row counts with the original.

Exercise

Take a messy CSV and write a cleaning function that applies these steps. Run it twice on fresh copies and confirm the output is identical both times.

Want the rest of the course? See the full Python for Data course page.

Educational content only. No income or earnings results are promised, and no certification, licence or credential is awarded.

Editorial & AI Disclosure