🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Machine Learning Notes
Topic #53

Data Cleaning

Data cleaning is the broad pass over raw data to fix structural problems — wrong data types, inconsistent text formatting, invalid values — before any deeper preprocessing like imputation or encoding happens.

The Cleaning Checklist

import pandas as pd

df = pd.read_csv("customers.csv")

# 1. Check dtypes — numbers stored as text are invisible bugs
print(df.dtypes)
df["income"] = pd.to_numeric(df["income"], errors="coerce")   # invalid values become NaN

# 2. Standardize inconsistent text categories
df["city"] = df["city"].str.strip().str.lower()
df["city"] = df["city"].replace({"delhi ncr": "delhi", "new delhi": "delhi"})

# 3. Check for invalid/impossible values
print(df[df["age"] < 0])          # negative ages — a data entry error
print(df[df["age"] > 120])         # implausible ages

# 4. Check column names
df.columns = df.columns.str.strip().str.lower().str.replace(" ", "_")

Why "lower/upper/delhi ncr" Text Inconsistency Matters

To a human, "Delhi", "delhi ", and "New Delhi" obviously mean the same thing. To a model — especially after one-hot encoding — these become three completely separate, unrelated categories, silently fragmenting your data and weakening any pattern the city feature could have provided.

Practical Use Cases

  • Cleaning survey data with inconsistent free-text entries
  • Fixing dtype issues after loading a CSV with mixed formatting
  • Catching data-entry errors (negative ages, impossible dates) before they corrupt a model silently

Common Mistakes

  • Assuming a column labeled numeric is actually numeric — currency symbols, commas, or stray text ("N/A" mixed into a numeric column) silently make a column an object dtype.
  • Cleaning text case-sensitively only halfway (fixing "Delhi"/"delhi" but missing "DELHI") — always normalize case and whitespace together.

Interview Relevance

Q: "How would you catch a data entry error like a negative age in a dataset?" Range/sanity checks after loading — df[df['age'] < 0] or df.describe()'s min/max — combined with domain knowledge about what a plausible value range actually is.

Practice Question

A "country" column contains the values "India", "india", "INDIA " and "Bharat" all meaning the same country. Write the Pandas code to normalize all of these into a single consistent value.

Want to go beyond the notes?

Join CodingNow 2.0's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Data Cleaning – FAQs

Quick answers about learning Data Cleaning in Machine Learning.

This free note from CodingNow 2.0 explains Data Cleaning in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine Learning topic on CodingNow 2.0, including Data Cleaning, is 100% free with no signup required.
With focused practice, most students grasp Data Cleaning in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now