Data cleaning is an essential part of data preprocessing. Before performing any analysis or building machine learning models, it is important to clean and transform the data to ensure accuracy, consistency, and completeness. In Python, the Pandas library provides powerful tools for handling missing values, correcting data types, removing duplicates, renaming columns, and more. This guide covers the core techniques and methods used for data cleaning in Pandas with detailed explanations and examples.
Data cleaning refers to identifying and correcting (or removing) corrupt or inaccurate records from a dataset. Common issues include:
Pandas offers a high-level abstraction for managing tabular data using the DataFrame structure, which makes it easy to identify and fix these problems.
import pandas as pd
# Load a dataset
df = pd.read_csv('data.csv')
# Preview the first few rows
print(df.head())
After loading the data, we can begin the data cleaning process.
Missing values are typically represented as NaN (Not a Number) in Pandas. To detect them:
# Detect missing values
print(df.isnull())
# Count total missing values in each column
print(df.isnull().sum())
Use dropna() to remove rows or columns with missing data.
# Drop rows with any missing values
df_cleaned = df.dropna()
# Drop columns with all missing values
df_cleaned = df.dropna(axis=1, how='all')
You can fill missing values using a fixed value or a method like forward-fill or backward-fill:
# Fill with a constant
df['Age'].fillna(0, inplace=True)
# Forward fill
df.fillna(method='ffill', inplace=True)
# Backward fill
df.fillna(method='bfill', inplace=True)
# Fill with column mean
df['Salary'].fillna(df['Salary'].mean(), inplace=True)
# Fill with column median
df['Age'].fillna(df['Age'].median(), inplace=True)
# Fill with column mode
df['Gender'].fillna(df['Gender'].mode()[0], inplace=True)
# Check data types
print(df.dtypes)
# Convert string to numeric
df['Salary'] = pd.to_numeric(df['Salary'], errors='coerce')
# Convert to datetime
df['JoinDate'] = pd.to_datetime(df['JoinDate'], errors='coerce')
# Convert to category
df['Department'] = df['Department'].astype('category')
# Check for duplicates
print(df.duplicated())
# Count duplicates
print(df.duplicated().sum())
# Remove duplicate rows
df = df.drop_duplicates()
print(df.columns)
# Rename specific columns
df.rename(columns={'EmpName': 'Employee_Name', 'Dept': 'Department'}, inplace=True)
# Rename all columns (e.g., make lowercase)
df.columns = [col.lower() for col in df.columns]
# Calculate IQR
Q1 = df['Salary'].quantile(0.25)
Q3 = df['Salary'].quantile(0.75)
IQR = Q3 - Q1
# Define outlier bounds
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
# Filter out outliers
df_no_outliers = df[(df['Salary'] >= lower_bound) & (df['Salary'] <= upper_bound)]
from scipy import stats
import numpy as np
# Z-score method
z_scores = np.abs(stats.zscore(df[['Salary']]))
df = df[(z_scores < 3).all(axis=1)]
# Replace specific values
df['Gender'].replace({'M': 'Male', 'F': 'Female'}, inplace=True)
# Replace using regex
df['Email'] = df['Email'].replace(r'\.com$', '.org', regex=True)
# Remove leading/trailing spaces
df['Name'] = df['Name'].str.strip()
# Convert to lowercase
df['Name'] = df['Name'].str.lower()
# Convert to uppercase
df['Name'] = df['Name'].str.upper()
# Replace substrings
df['Address'] = df['Address'].str.replace('Street', 'St')
# Convert to category and inspect
df['Department'] = df['Department'].astype('category')
print(df['Department'].cat.categories)
# Rename categories
df['Department'].cat.rename_categories({'HR': 'Human Resources', 'IT': 'Tech'}, inplace=True)
df['JoinDate'] = pd.to_datetime(df['JoinDate'], errors='coerce')
# Extract year, month, day
df['Year'] = df['JoinDate'].dt.year
df['Month'] = df['JoinDate'].dt.month
df['Day'] = df['JoinDate'].dt.day
# Split name column into first and last names
df[['FirstName', 'LastName']] = df['Name'].str.split(' ', 1, expand=True)
# Define function to standardize salary
def clean_salary(salary):
if pd.isna(salary):
return 0
return float(salary.replace('$', '').replace(',', ''))
df['Salary'] = df['Salary'].apply(clean_salary)
# Save to new CSV
df.to_csv('cleaned_data.csv', index=False)
import pandas as pd
# Load raw data
df = pd.read_csv('employees_raw.csv')
# Drop duplicates
df = df.drop_duplicates()
# Strip whitespaces
df['Name'] = df['Name'].str.strip()
# Fill missing salaries with median
df['Salary'].fillna(df['Salary'].median(), inplace=True)
# Convert JoinDate to datetime
df['JoinDate'] = pd.to_datetime(df['JoinDate'], errors='coerce')
# Handle incorrect genders
df['Gender'] = df['Gender'].replace({'M': 'Male', 'F': 'Female', 'male': 'Male', 'female': 'Female'})
# Remove outliers in Salary
Q1 = df['Salary'].quantile(0.25)
Q3 = df['Salary'].quantile(0.75)
IQR = Q3 - Q1
lower = Q1 - 1.5 * IQR
upper = Q3 + 1.5 * IQR
df = df[(df['Salary'] >= lower) & (df['Salary'] <= upper)]
# Save cleaned data
df.to_csv('employees_clean.csv', index=False)
Data cleaning is a crucial skill in data science and analytics. Pandas offers comprehensive tools for handling missing values, fixing data types, removing duplicates, parsing dates, cleaning text, handling outliers, and more. Clean data ensures accurate, reliable analysis and modeling. Mastering data cleaning with Pandas equips you to tackle real-world datasets with confidence and efficiency.
Python is commonly used for developing websites and software, task automation, data analysis, and data visualisation. Since it's relatively easy to learn, Python has been adopted by many non-programmers, such as accountants and scientists, for a variety of everyday tasks, like organising finances.
Learning Curve: Python is generally considered easier to learn for beginners due to its simplicity, while Java is more complex but provides a deeper understanding of how programming works.
The point is that Java is more complicated to learn than Python. It doesn't matter the order. You will have to do some things in Java that you don't in Python. The general programming skills you learn from using either language will transfer to another.
Read on for tips on how to maximize your learning. In general, it takes around two to six months to learn the fundamentals of Python. But you can learn enough to write your first short program in a matter of minutes. Developing mastery of Python's vast array of libraries can take months or years.
6 Top Tips for Learning Python
The following is a step-by-step guide for beginners interested in learning Python using Windows.
Best YouTube Channels to Learn Python
Write your first Python programStart by writing a simple Python program, such as a classic "Hello, World!" script. This process will help you understand the syntax and structure of Python code.
The average salary for Python Developer is βΉ5,55,000 per year in the India. The average additional cash compensation for a Python Developer is within a range from βΉ3,000 - βΉ1,20,000.
Copyrights © 2024 letsupdateskills All rights reserved