Pandas — Data Cleaning
Detect missing values
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, 2, np.nan], 'B': [np.nan, 2, 3]})
print(df.isnull())
print(df.isnull().sum())
Drop missing values
df.dropna() # Drop rows with any NaN
df.dropna(subset=['A']) # Drop rows where A is NaN
df.dropna(thresh=2) # Drop rows with less than 2 non-NaN
Fill missing values
df.fillna(0) # Fill with 0
df.fillna(df.mean()) # Fill with mean
df.fillna(method='ffill') # Forward fill
df.fillna(method='bfill') # Backward fill
Interpolate
df.interpolate()
Replace
df.replace({np.nan: 0})
Mini Practice
- Detect missing values
- Drop missing rows
- Fill missing values
- Use interpolation
Up Next
Continue with Duplicates - Removing duplicates.
Related Topics
Frequently Asked Questions about Data Cleaning
What is Data Cleaning in Pandas?
Data Cleaning is a fundamental concept in Pandas. 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 Pandas?
Data Cleaning is essential for Pandas development. Understanding this concept will help you write better code and solve real-world problems more effectively.