Overview of data cleaning and transformation techniques for preparing datasets for reliable machine learning, including AWS tools and practical examples.
Models only learn from the data you provide. If that data is inaccurate, inconsistent, or full of errors, the model will be unreliable. In short: bad data equals bad models. Data cleaning and transformation are essential steps that prevent poor-quality data from undermining the entire machine learning pipeline.Data cleaning identifies and fixes problems such as missing values, inconsistent formats, duplicates, outliers, and type mismatches. Once cleaned, data is transformed to meet algorithm requirements (scaling, encoding, aggregation, parsing, binning), producing a reliable foundation for feature engineering and modeling.
Clean, well-documented data is the first and most important step toward building reliable ML systems. Invest time here to avoid compounding issues downstream.
Quick reference — common problems and actions
Problem
Typical fixes
Missing values
Drop rows/columns, impute (mean/median/mode), or model-based imputation
Duplicates
Remove exact duplicates or dedupe based on key subset
Inconsistent formats
Parse and normalize (e.g., dates to ISO 8601)
Outliers
Detect with IQR/domain thresholds; cap, transform, or remove
Type mismatches
Strip non-numeric characters and cast to numeric/datetime
Categorical inconsistencies
Map variants/typos to a controlled vocabulary
Common cleaning techniques (expanded)
Missing values: remove or impute (median is robust for skewed data).
Duplicates: deduplicate using full-row or subset-based logic.
Outliers: apply domain thresholds, IQR, or winsorization.
Type conversions: ensure numeric, datetime, and categorical dtypes are correct.
Controlled vocabulary: normalize typos and synonyms to canonical category values.
Missing values
Missing values can distort statistics and models. Choose between removing affected rows/columns (simpler, destructive) or imputing (preserves sample size). Imputation methods include mean, median, mode, forward/backfill, or model-based techniques.
Dropping rows with missing values is irreversible for that dataset copy and can bias results if missingness is not random. Prefer imputation or analyze missingness patterns first.
Example — drop or impute with pandas:
import pandas as pddf = pd.DataFrame({ "Name": ["Alice", "Bob", "Charlie"], "Age": [29, None, 45]})# Drop rows with any missing valuesdf_dropped = df.dropna()# Fill missing Age with median (robust to skew)median_age = df["Age"].median()df_filled = df.copy()df_filled["Age"] = df_filled["Age"].fillna(median_age)
Inconsistent formats
Canonicalizing formats (especially dates and numeric strings) prevents parsing errors and ensures consistent sorting, filtering, and aggregation. Use robust parsing functions that can coerce invalid values to null for later inspection.Example — parse dates and format as ISO 8601 with pandas:
df["timestamp"] = pd.to_datetime(df["timestamp"], errors="coerce", infer_datetime_format=True)# To format as ISO string:df["timestamp_iso"] = df["timestamp"].dt.strftime("%Y-%m-%dT%H:%M:%S")
Duplicates
Duplicates inflate counts and skew model training. Remove exact duplicates or deduplicate using a subset of columns (e.g., id + timestamp), depending on the domain.
# Remove exact duplicate rowsdf_unique = df.drop_duplicates()# Remove duplicates based on subset of columns (e.g., id and timestamp)df_unique_subset = df.drop_duplicates(subset=["id", "timestamp"])# In-place single-line usagedf.drop_duplicates(inplace=True)
Typos and inconsistent categorical values
Create a controlled vocabulary (mapping table) to normalize synonyms, abbreviations, and typos into a single canonical value. This improves feature consistency and reduces cardinality.Example mapping:
mapping = { "US": "United States", "USA": "United States", "United States of America": "United States", "U.S.": "United States"}df["country_clean"] = df["country"].replace(mapping)
Outliers
Outliers can be legitimate or erroneous. Detect with domain thresholds, Z-score, or IQR. Handling strategies include removing, capping, or transforming the values.Example — winsorization by clipping to 1st and 99th percentiles:
low, high = df["value"].quantile([0.01, 0.99])df["value_clipped"] = df["value"].clip(lower=low, upper=high)
Data type mismatches
Numeric values sometimes contain currency symbols, commas, or other non-numeric characters. Strip those characters and cast to numeric or datetime types. Coerce invalid parses to NaN for later handling.Example — clean dollar amounts and convert to numeric:
import pandas as pd# Remove non-numeric characters and convert to numeric (coerce errors to NaN)df["price_clean"] = pd.to_numeric( df["price"].astype(str) .str.replace(r"[^\d\.\-]", "", regex=True) .replace("", pd.NA), errors="coerce")
Data transformation
Cleaning fixes errors; transformation prepares features for learning algorithms. Typical transformations include scaling, encoding, aggregation, parsing, and binning.
Key transformation techniques
Scaling / Normalization
Rescale numeric features so ones with large ranges don’t dominate models. Use Min-Max scaling to rescale to [0, 1], or standardization (Z-score) to center to mean 0 and unit variance.
Binning (discretization)
Binning converts continuous variables into discrete intervals to reduce noise or improve interpretability. Use pd.cut for fixed bins or pd.qcut for quantiles.
Note: pd.cut’s right parameter controls whether the bins include the right edge; adjust right=True/False or bin edges to match your intended inclusive boundaries.
Binning simplifies patterns and improves interpretability at the cost of some granularity. Choose bins with domain knowledge or data-driven methods.AWS tools for cleaning and transformation
AWS offers managed services that streamline cleaning and transformation within ML workflows:
Service
Purpose
AWS Glue DataBrew
Visual, no-code/low-code data cleaning and profiling
AWS Glue
Serverless ETL for automated, scalable transformations
Amazon EMR
Distributed processing (Spark/Hadoop) for large-scale transformations
SageMaker Data Wrangler
Integrated data preparation and visualization inside SageMaker