Data Cleaning
Complete learning notes
1. Introduction
Real-world data is messy — inconsistent text capitalization, extra whitespace, wrong data types, and irrelevant columns are the norm, not the exception. Data Cleaning is the broad process of fixing these issues before a dataset is usable for analysis or modeling. This topic covers general cleaning techniques; the following topics (Missing Values, Duplicates, Outliers) dive into specific, more focused cleaning tasks.
2. What is Data Cleaning?
Simple definition: Data cleaning is the process of fixing or removing incorrect, inconsistent, irrelevant, or improperly formatted data so that a dataset is accurate and ready for analysis.
Technical explanation: Data cleaning encompasses a range of operations — correcting data types, standardizing text formatting, fixing inconsistent category labels, removing irrelevant columns, and validating data against expected ranges or formats — performed before deeper preprocessing steps like handling missing values or encoding.
3. Why is it Important?
- "Garbage in, garbage out" — even the best ML algorithm cannot produce good results from messy, inconsistent data.
- Real-world datasets are rarely clean; cleaning is often the most time-consuming part of any ML project.
- Clean data leads to more reliable, trustworthy analysis and modeling results.
4. Prerequisites
Comfort with Pandas basics (Module 1, Topic 6) and Loading Datasets (Topic 1 of this module).
5. Core Concepts
- Fixing incorrect data types
- Standardizing text (case, whitespace)
- Fixing inconsistent category labels
- Removing irrelevant columns
- Validating data against expected values/ranges
6. Detailed Explanation
a) Fixing Incorrect Data Types
Sometimes numeric columns are accidentally loaded as text (e.g., "25" instead of 25), which prevents proper mathematical operations. astype() converts a column to the correct data type.
b) Standardizing Text
Inconsistent capitalization or extra whitespace (e.g., "New York ", "new york", "NEW YORK") can cause what should be identical values to be treated as different categories. Methods like .str.strip() and .str.lower() fix this.
c) Fixing Inconsistent Category Labels
Sometimes the same category is recorded inconsistently (e.g., "Male", "M", "male" all meaning the same thing). The .replace() method can standardize these into a single consistent label.
d) Removing Irrelevant Columns
Columns that provide no useful information for your analysis or model (e.g., an internal tracking ID unrelated to the actual problem) can be safely dropped using df.drop(columns=[...]).
e) Validating Data Against Expected Ranges
Checking that values fall within reasonable, expected ranges (e.g., age should not be negative or over 120) helps catch data entry errors early.
7. How It Works
- Inspect the dataset closely (
head(),info(),unique()on categorical columns) to identify issues. - Fix data type mismatches using
astype(). - Standardize text formatting using
.str.strip(),.str.lower(), or similar methods. - Standardize inconsistent category labels using
.replace(). - Drop clearly irrelevant columns.
- Re-inspect the cleaned data to confirm the fixes worked as expected.
8. Real-World Example
Imagine a survey dataset where the "Gender" column contains a mix of "Male", "male", "M", "Female", "female", and "F". Without cleaning, a model (or even a simple value_counts()) would treat these as six separate categories instead of two. Data cleaning standardizes these into consistent labels like "Male" and "Female" before any further analysis.
9. Python Example
pythonimport pandas as pd data = { "name": [" Aarav", "Meera ", "KABIR", "diya"], "age": ["21", "20", "22", "19"], "gender": ["M", "Female", "male", "F"], "internal_id": [1001, 1002, 1003, 1004] } df = pd.DataFrame(data) print("Before cleaning:") print(df) print(df.dtypes) # Fixing data type df["age"] = df["age"].astype(int) # Standardizing text (strip whitespace, fix case) df["name"] = df["name"].str.strip().str.title() # Fixing inconsistent category labels df["gender"] = df["gender"].replace({ "M": "Male", "male": "Male", "F": "Female", "female": "Female" }) # Removing an irrelevant column df = df.drop(columns=["internal_id"]) print("\nAfter cleaning:") print(df) print(df.dtypes)
Expected Output:
textBefore cleaning: name age gender internal_id 0 Aarav 21 M 1001 1 Meera 20 Female 1002 2 KABIR 22 male 1003 3 diya 19 F 1004 name object age object gender object internal_id int64 dtype: object After cleaning: name age gender 0 Aarav 21 Male 1 Meera 20 Female 2 Kabir 22 Male 3 Diya 19 Female name object age int64 gender object dtype: object
10. Code Explanation
df["age"].astype(int)converts the "age" column from text to integer, enabling proper numeric operations going forward.df["name"].str.strip().str.title()removes leading/trailing whitespace and standardizes capitalization (e.g.,"KABIR"→"Kabir").df["gender"].replace({...})maps all inconsistent gender labels to two standardized values,"Male"and"Female".df.drop(columns=["internal_id"])removes a column that provides no analytical value, keeping the dataset focused and clean.- Comparing the
dtypesbefore and after confirms thatageis now properly numeric (int64).
11. Advantages
- Ensures consistency, making group-by operations, filtering, and modeling far more reliable.
- Prevents subtle bugs caused by mismatched data types or inconsistent category labels.
- Improves the overall quality and trustworthiness of any downstream analysis.
12. Limitations
- Manual cleaning can be time-consuming, especially for very large or highly inconsistent datasets.
- Some cleaning decisions require domain knowledge (e.g., what counts as a "reasonable" age range) that isn't purely a coding problem.
- Overly aggressive cleaning (e.g., dropping too many columns) can accidentally remove genuinely useful information.
13. Common Mistakes
- Assuming a dataset is clean just because it loaded without errors.
- Forgetting to check for inconsistent capitalization or whitespace in categorical columns, leading to inflated "unique category" counts.
- Converting data types without first checking for invalid values (e.g., trying to convert
"unknown"directly to an integer, causing an error). - Dropping columns without confirming they are genuinely irrelevant to the analysis.
14. Best Practices
- Use
.unique()or.value_counts()on categorical columns early to spot inconsistent labels. - Always verify data types with
df.dtypesordf.info()after cleaning. - Document (via comments) why specific columns were dropped or specific labels were standardized.
- Clean data in a logical order: fix types → standardize text → fix categories → drop irrelevant columns.
15. Real-World Applications
- Preparing survey data with inconsistent free-text or categorical responses for analysis.
- Cleaning scraped web data, which often contains inconsistent formatting and extra whitespace.
- Standardizing merged datasets from multiple sources that may label the same categories differently.
16. Interview-Oriented Points
- Be ready to explain why "garbage in, garbage out" applies strongly to ML.
- Understand common cleaning operations: type conversion, text standardization, category consistency, irrelevant column removal.
- Be able to describe how you'd detect inconsistent categorical labels in a new dataset.
17. Exam-Oriented Points
- Data cleaning fixes incorrect data types, inconsistent text, inconsistent categories, and irrelevant columns.
.astype()converts data types;.str.strip()and.str.lower()/.str.title()standardize text;.replace()fixes inconsistent labels.df.drop(columns=[...])removes irrelevant columns.
18. Comparison Table — Data Cleaning vs Data Preprocessing (Scope)
| Aspect | Data Cleaning | Data Preprocessing (Broader) |
|---|---|---|
| Scope | Fixing incorrect/inconsistent/irrelevant data | The entire pipeline: cleaning + missing values + duplicates + outliers + encoding + scaling + selection + splitting |
| Focus | Correctness and consistency of existing data | Preparing data comprehensively for ML modeling |
| Relationship | One step within preprocessing | The overall process, of which cleaning is the first major step |
19. Quick Revision
- Data Cleaning fixes incorrect data types, inconsistent text formatting, inconsistent category labels, and removes irrelevant columns.
.astype(),.str.strip(),.str.lower()/.str.title(), and.replace()are core cleaning tools.- Always inspect data with
.unique()or.value_counts()to spot inconsistencies before modeling. - Data cleaning is the essential first step of the broader Data Preprocessing pipeline.