Data scientists spend 80% of their time cleaning data, not analyzing it. Real-world datasets arrive with missing values, inconsistent formats, duplicate records, and outright errors. Here’s a systematic workflow using Python Pandas to turn messy data into analysis-ready datasets.
1. Load and Inspect
Start by loading your dataset and getting a feel for its shape, column types, and basic statistics.
import pandas as pd
import numpy as np
df = pd.read_csv("messy_data.csv")
print(df.shape)
print(df.info())
print(df.describe(include='all'))
print(df.head())
2. Handle Missing Values
First, visualize missing data patterns. Then decide on a strategy: drop rows with critical missing fields, impute numeric columns with median, and fill categorical columns with mode or “Unknown”.
# Check missing values print(df.isnull().sum()) # Strategies df['age'].fillna(df['age'].median(), inplace=True) df['category'].fillna(df['category'].mode()[0], inplace=True) df.dropna(subset=['email'], inplace=True) # critical field
3. Standardize Data Types
Dates often arrive as strings. Prices may include currency symbols. Use pd.to_datetime() and pd.to_numeric() with error coercion.
df['date'] = pd.to_datetime(df['date'], errors='coerce')
df['price'] = df['price'].replace('[$,]', '', regex=True)
df['price'] = pd.to_numeric(df['price'], errors='coerce')
“Clean data isn’t an optional preprocessing step. It’s the difference between a model that works and a model that quietly produces wrong answers.”
4. Remove Duplicates
Duplicate rows skew statistics and double-count records. Use df.duplicated() to find them and df.drop_duplicates() with subset parameters for nuanced deduplication.
5. Standardize Text Fields
Lowercase all strings, strip whitespace, and correct common typos using fuzzy matching. Create a mapping dictionary for known inconsistencies (e.g., “USA”, “U.S.A.”, “United States” → “US”).
df['country'] = df['country'].str.strip().str.lower()
corrections = {'u.s.a.': 'us', 'usa': 'us', 'united states': 'us'}
df['country'] = df['country'].replace(corrections)
6. Detect Outliers
Use IQR or Z-score methods to flag outliers. Investigate before removing — outliers may be legitimate extreme values or data entry errors.
7. Export Clean Data
Save your cleaned dataset as Parquet (faster, smaller) or CSV. Create a data quality report summarizing what was cleaned, how many rows were dropped, and imputation statistics.
df.to_parquet("clean_data.parquet", index=False)
# Generate quality report
report = {
"original_rows": len(original_df),
"cleaned_rows": len(df),
"missing_filled": missing_cols,
"duplicates_removed": dup_count
}