Skip to content

tutorials · tutorial

Clean a scraped table with pandas in 20 lines

Parse prices, normalise whitespace, drop duplicates on a key and report fill rates — the four steps that make scraped data usable.

4 min readPublished 2026-11-25Updated 2026-11-25Reviewed by a person, not a template
Jump to the short answer
On this page
  1. The 20 lines
  2. The rules behind it
  3. When pandas stops being enough

The short answer

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.

Want this data without building it?

Free 100-row sample with fill rates, then a written quote in 24 hours.

Get a quote

Questions this raises

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.

Scrapefall 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.

Reviewed by a second engineer before publishing · last updated 2026-11-25

All articles

Send one URL. Get one sample. Decide from data, not a sales call.

100-row sample, masked where needed, with the source URL on every row. Written quote within 24 hours.