Clean a scraped table with pandas in 20 lines
Updated 2026-11-25 · 4 min read · By BYM Data team
Scraped tables are strings pretending to be data. Four operations fix most of it: trim and collapse whitespace, parse numbers and dates explicitly, drop duplicates on a real key, and print a fill-rate report so you know what you actually have.
The 20 lines
import pandas as pd, re
df = pd.read_csv("raw.csv", dtype=str)
# 1. whitespace + unicode noise
df = df.apply(lambda s: s.str.replace(r"s+", " ", regex=True).str.strip())
# 2. parse numbers and dates explicitly
df["price"] = (df["price"].str.replace(r"[^0-9.]", "", regex=True).astype(float))
df["mrp"] = (df["mrp"].str.replace(r"[^0-9.]", "", regex=True).astype(float))
df["updated_at"] = pd.to_datetime(df["updated_at"], errors="coerce", utc=True)
# 3. dedupe on the natural key, keep the freshest
df = df.sort_values("updated_at").drop_duplicates("sku", keep="last")
# 4. fill-rate report
report = (df.notna().mean().mul(100).round(1).sort_values(ascending=False))
print(report.to_string())
print(f"{len(df):,} rows · {df.sku.nunique():,} unique SKUs")The rules behind it
- Never let pandas infer types from scraped text
- Parse currency by stripping non-numeric characters, then validate the range
- Always dedupe on a key you chose, not on the whole row
- Print fill rates before and after cleaning — the delta is your data-quality evidence
When pandas stops being enough
Past a few million rows or when several files must reconcile, move to a database with a staging table and merge — the same logic, better memory behaviour.
Questions
Why errors='coerce' on dates?
So a malformed value becomes NaT instead of crashing the job; then count the NaT rows and treat them as a fill-rate problem.
Should I keep the raw file?
Yes — always. Cleaning must be reproducible from raw.
How do I handle multi-currency?
Store amount and currency separately; never sum mixed currencies.
BYM Data team
Data engineering — We build and run the extraction pipelines behind every dataset we sell. Guides are written by whoever maintains the source, and reviewed by a second engineer before publishing.