Python Data Cleaning Cheat Sheet

๐Ÿ“ŒInspect the Dataset

Understand the structure, data types, missing values, and basic statistics.

df.head()
df.info()
df.describe()

๐Ÿ“ŒCheck Missing Values

Count missing values across every column before deciding how to handle them.

df.isnull().sum()  # Total missing values
df.isnull().mean() * 100  # Percentage of missing values

๐Ÿ“ŒRemove Missing Values

Delete rows or columns when missing data cannot be meaningfully recovered.

df.dropna()  # Remove rows with missing values
df.dropna(subset=["email"])  # Remove rows with missing values in the "email" column
df.dropna(axis=1)  # Remove columns with missing values

๐Ÿ“ŒFill Missing Values

Replace missing values using constants, averages, medians, or previous values.

df["age"].fillna(df["age"].median())  # Fill with median
df["city"].fillna("Unknown")  # Fill with a constant
df.ffill()  # Forward fill

๐Ÿ“ŒRemove Duplicate Records

Identify and remove repeated rows that may distort analysis.

df.duplicated().sum()  # Count duplicates
df.drop_duplicates()  # Remove duplicates
df.drop_duplicates(subset=["customer_id"])  # Remove duplicates based on "customer_id"

๐Ÿ“ŒFix Data Types

Convert columns into the correct numerical, text, or datetime format.

df["price"] = pd.to_numeric(df["price"], errors="coerce")  # Convert to numeric
df["date"] = pd.to_datetime(df["date"])  # Convert to datetime
df["category"] = df["category"].astype("category")  # Convert to category

๐Ÿ“ŒClean Column Names

Standardize column names to make filtering and coding easier.

df.columns = df.columns.str.strip()  # Remove leading/trailing spaces
df.columns = df.columns.str.lower()  # Convert to lowercase
df.columns = df.columns.str.replace(" ", "_")  # Replace spaces with underscores

๐Ÿ“ŒClean Text Values

Remove spaces, normalize capitalization, and replace inconsistent labels.

df["name"] = df["name"].str.strip()  # Remove leading/trailing spaces
df["city"] = df["city"].str.title()  # Capitalize city names
df["status"] = df["status"].replace({"Yes": "Y", "No": "N"})  # Replace values

๐Ÿ“ŒDetect Outliers

Find unusually high or low values using the interquartile range.

q1 = df["salary"].quantile(0.25)
q3 = df["salary"].quantile(0.75)
iqr = q3 - q1
outliers = (df["salary"] < q1 - 1.5 * iqr) | (df["salary"] > q3 + 1.5 * iqr)

๐Ÿ“ŒHandle Outliers

Remove, cap, or transform extreme values based on business context.

lower = q1 - 1.5 * iqr
upper = q3 + 1.5 * iqr
df["salary"] = df["salary"].clip(lower, upper)  # Cap values

๐Ÿ“ŒStandardize Categories

Fix inconsistent spelling, capitalization, and category labels.

df["department"] = df["department"].str.lower()  # Normalize capitalization
df["department"] = df["department"].replace({"human resources": "hr"})  # Replace labels

๐Ÿ“ŒValidate Value Ranges

Check whether numerical values fall within realistic boundaries.

df = df[df["age"].between(18, 100)]  # Filter age between 18 and 100
df = df[df["quantity"] > 0]  # Ensure quantity is positive
df = df[df["rating"].between(1, 5)]  # Ensure ratings are between 1 and 5

๐Ÿ“ŒSplit and Extract Text

Separate combined fields or extract useful patterns from text.

df[["first_name", "last_name"]] = df["full_name"].str.split(" ", expand=True)  # Split names
df["email_domain"] = df["email"].str.extract(r"@(.+)$")  # Extract email domains

๐Ÿ“ŒRename and Reorder Columns

Create clearer labels and organize columns for easier analysis.

df.rename(columns={"amt": "amount"}, inplace=True)  # Rename columns
df = df[["customer_id", "date", "amount", "status"]]  # Reorder columns

๐Ÿ“ŒReset the Index

Create a clean sequential index after removing or filtering rows.

df = df.reset_index(drop=True)

๐Ÿ“ŒFinal Quality Check

Confirm the dataset is complete, consistent, and analysis-ready.

df.info()  # Check structure
df.isnull().sum()  # Check missing values
df.duplicated().sum()  # Check duplicates
df.describe(include="all")  # Check statistics

๐Ÿ“ŒRecommended Cleaning Workflow

Inspect
โ†ท
Standardize
โ†ท
Handle Missing Data
โ†ท
Remove Duplicates
โ†ท
Fix Types
โ†ท
Export
โ†ท
Validate
โ†ท
Treat Outliers
df.to_csv("cleaned_data.csv", index=False)

๐Ÿ“ŒReal-Time Story: ๐Ÿงน Cleaning Data Like a Pro

Imagine you're a data analyst working for an e-commerce company. Your manager hands you a messy dataset containing customer orders. Here's how you clean it step by step:

Inspect the Dataset: You start by checking the structure and identifying missing values. You notice that some columns, like "email" and "price," have gaps.
Handle Missing Values: You decide to fill missing prices with the median and drop rows where "email" is missing since it's critical for customer communication.
Remove Duplicates: You find duplicate entries for some customers and remove them to ensure accurate analysis.
Fix Data Types: The "date" column is stored as text, so you convert it to a datetime format for better filtering.
Standardize Categories: You notice inconsistent department names like "Human Resources" and "HR," so you standardize them.
Validate Value Ranges: You filter out orders with negative quantities and ensure ratings are between 1 and 5.
Final Check: After cleaning, you save the cleaned dataset and present it to your manager, ready for analysis.

By following these steps, you ensure the data is reliable, consistent, and ready for actionable insights!