Skip to content

Cleaning Messy Data with Pandas: A Repeatable Workflow

A step-by-step approach to turning an untidy spreadsheet into analysis-ready data, with code you can rerun the next time the file arrives.

Muhammad Yasir

@YasirAwan4831

Published 2 min read

Cleaning Messy Data with Pandas: A Repeatable Workflow

On this page

Analysts often spend more time cleaning data than analyzing it. The trick is to make cleaning repeatable: a script you can run again when next month's file arrives, rather than manual edits you have to remember.

Start by looking, not changing

import pandas as pd

df = pd.read_csv("sales.csv")
print(df.shape)
print(df.dtypes)
print(df.isna().sum())
print(df.head())

Check the number of rows and columns, the data types Pandas guessed, and how many values are missing in each column. Most problems announce themselves here.

A reliable cleaning sequence

  1. Standardize column names: lowercase, no spaces.

  2. Fix types: convert dates and numbers explicitly.

  3. Handle missing values deliberately: drop, fill or flag.

  4. Remove duplicates using the columns that define a unique record.

  5. Normalize text: trim whitespace and unify spelling variants.

  6. Validate: check ranges and totals against something you trust.

df.columns = df.columns.str.strip().str.lower().str.replace(" ", "_")

df["order_date"] = pd.to_datetime(df["order_date"], errors="coerce")
df["amount"] = pd.to_numeric(df["amount"], errors="coerce")

df["city"] = df["city"].str.strip().str.title()
df = df.drop_duplicates(subset=["order_id"])
df = df.dropna(subset=["order_date", "amount"])

Be explicit about missing data

Dropping rows is the easiest option and sometimes the wrong one. Ask why values are missing. If "no discount" appears as an empty cell, filling with zero is correct; if a customer's age is unknown, filling with zero would be a lie. When unsure, add a flag column so the decision stays visible.

Validate before you trust it

  • Do totals match the figures in the source system?

  • Are dates inside the expected range?

  • Are there negative amounts where none should exist?

  • Did the row count change by the amount you expected?

Make it rerunnable

Wrap the steps in a function, read the file path from an argument and save the cleaned output to a new file. Keep the raw data untouched. When the next file arrives, you run one command instead of repeating an afternoon of clicking.

Enjoyed this article?

Share it with someone who might find it useful.