pandas cleanup guide

pandas Data Cleaning

Clean pandas datasets by fixing dtypes, normalizing category labels, handling missing values, and deduplicating records with a defined business key.

How this page is maintained

Written for learners, checked against the sources below, and reviewed every year. Last reviewed July 22, 2026.

Short answer

In pandas, clean data in a fixed order: inspect, standardize types, handle missing values, normalize categorical labels, then remove duplicates by business key. Validate counts after each step so cleanup decisions remain auditable.

  • Use DataFrame.info and value_counts to profile before editing.
  • Convert dates and numeric fields early to avoid silent string behavior.
  • Drop duplicates only after defining a real uniqueness key.

Profile and normalize the orders dataset

Start with orders.csv that contains order_id, order_date, channel, amount, and status. Check dtypes and missing values, then normalize channel labels like Web, web, and WEB into one standard value.

Use pd.to_datetime for date fields and pd.to_numeric with explicit error handling for numeric columns such as amount.

Handle missing values and duplicates deliberately

Missing values can mean unknown, not zero. Keep that meaning in mind when filling or dropping rows. For duplicate order IDs, verify whether they are true repeats or partial updates before removing records.

  • Keep raw and cleaned DataFrames separate during development.
  • Track how many rows each cleanup step changes.
  • Persist a small QA sample for manual inspection.

Standardize and deduplicate order records

A DataFrame loaded from orders.csv includes inconsistent channel labels and repeated order IDs.

  1. Run df["channel"] = df["channel"].str.strip().str.lower() to normalize labels.
  2. Convert df["order_date"] with pd.to_datetime and df["amount"] with pd.to_numeric.
  3. Drop duplicates on order_id after reviewing collisions and keep the latest timestamp.
Result: The cleaned DataFrame has consistent types, normalized categories, and one row per order ID.

Common mistakes

  • Filling all nulls with zero without checking business meaning.
  • Dropping duplicates before normalizing text and types.
  • Chaining many transforms without checking intermediate row counts.
  • Editing the only raw copy and losing traceability.

Try one

Why should type conversion happen before most aggregations in pandas?

Because string-typed numeric or date fields can produce wrong groupings and calculations.

Sources

Learn this with a tutor

Tell LearnLive what you already know and what you need to do with pandas cleaning.

Build this course