Introduction
Data cleaning and preprocessing are critical steps in any data analysis or machine learning workflow. Raw data is often incomplete, inconsistent, or noisy, which can lead to inaccurate results if not handled properly. Using Python and Pandas, developers can efficiently clean, transform, and prepare data for analysis and modeling.
This article explains how to clean and preprocess data in Python using Pandas step by step with practical examples and best practices.
What is Data Cleaning and Preprocessing?
Data cleaning involves handling missing values, removing duplicates, correcting errors, and ensuring consistency. Preprocessing includes transforming data into a suitable format for analysis or machine learning.
Why is Data Preprocessing Important?
Improves data quality
Enhances model accuracy
Reduces noise and inconsistencies
Ensures reliable insights
Prerequisites
Make sure you have the required libraries installed:
pip install pandas numpy
Step 1: Import Libraries
import pandas as pd
import numpy as np
Step 2: Load the Dataset
df = pd.read_csv("data.csv")
print(df.head())
Step 3: Understand the Data
print(df.info())
print(df.describe())
print(df.isnull().sum())
This helps identify missing values, data types, and overall structure.
Step 4: Handle Missing Values
Remove Missing Values
df = df.dropna()
Fill Missing Values
df['Age'].fillna(df['Age'].mean(), inplace=True)
Step 5: Remove Duplicates
df = df.drop_duplicates()
Step 6: Rename Columns
df.rename(columns={'old_name': 'new_name'}, inplace=True)
Step 7: Convert Data Types
df['Date'] = pd.to_datetime(df['Date'])
Step 8: Handle Outliers
q1 = df['Salary'].quantile(0.25)
q3 = df['Salary'].quantile(0.75)
iqr = q3 - q1
lower_bound = q1 - 1.5 * iqr
upper_bound = q3 + 1.5 * iqr
df = df[(df['Salary'] >= lower_bound) & (df['Salary'] <= upper_bound)]
Step 9: Encode Categorical Data
df = pd.get_dummies(df, columns=['Gender'], drop_first=True)
Step 10: Feature Scaling
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
df[['Salary']] = scaler.fit_transform(df[['Salary']])
Step 11: Save Cleaned Data
df.to_csv("cleaned_data.csv", index=False)
Join the conversation! Your thoughts help the community grow.