Data Science — Data Cleaning
Handle missing values
import pandas as pd
import numpy as np
# Drop missing values
df.dropna(inplace=True)
# Fill with mean/median/mode
df.fillna(df.mean(), inplace=True)
df.fillna(df.median(), inplace=True)
df.fillna(df.mode().iloc[0], inplace=True)
# Forward/backward fill
df.fillna(method='ffill', inplace=True)
df.fillna(method='bfill', inplace=True)
Remove duplicates
df.drop_duplicates(inplace=True)
Handle outliers
# IQR method
Q1 = df['column'].quantile(0.25)
Q3 = df['column'].quantile(0.75)
IQR = Q3 - Q1
lower = Q1 - 1.5 * IQR
upper = Q3 + 1.5 * IQR
df = df[(df['column'] >= lower) & (df['column'] <= upper)]
Fix data types
df['date'] = pd.to_datetime(df['date'])
df['category'] = df['category'].astype('category')
df['numeric'] = pd.to_numeric(df['numeric'], errors='coerce')
String cleaning
df['text'] = df['text'].str.lower()
df['text'] = df['text'].str.strip()
df['text'] = df['text'].str.replace(r'[^\w\s]', '', regex=True)
Mini Practice
- Handle missing values
- Remove duplicates
- Detect outliers
- Fix data types
Up Next
Continue with Time Series - Temporal data analysis.
Related Topics
Frequently Asked Questions about Data Cleaning
What is Data Cleaning in Data Science?
Data Cleaning is a fundamental concept in Data Science. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Data Cleaning?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Data Cleaning.
Why is Data Cleaning important in Data Science?
Data Cleaning is essential for Data Science development. Understanding this concept will help you write better code and solve real-world problems more effectively.