Pandas Data Cleaning: Automate Spreadsheets
Turn a weekly manual spreadsheet clean-up into a repeatable pandas script with safe type conversion, deliberate deduplication, validation and a rejects file.

Automate pandas data cleaning: standardise columns, convert dates and numbers safely, remove duplicates, validate and send bad rows to a rejects file.
TL;DR: Pandas data cleaning turns a recurring spreadsheet clean-up into a Python script. Profile the data, standardise column names, convert dates and numbers with errors coerced, deduplicate on a chosen key, standardise categories and validate rows. Send rows that fail checks to a rejects file for review instead of deleting them, then schedule the script.
Key takeaways
- Profile before writing rules.
- Coerce type errors to missing, then review.
- Sort, then drop_duplicates with keep='first'.
- Never delete bad rows: write a rejects file.
- Schedule and log the script; graduate to a pipeline when needed.
Many business processes still depend on spreadsheets exported from one system, cleaned by hand and imported into another. Someone fixes inconsistent dates, removes duplicate customers, trims stray spaces and converts text to numbers, every week, in the same way. Pandas data cleaning turns that repeated manual work into a short Python script that runs the same steps every time, flags rows it cannot fix and produces a clean file ready for import. This guide explains the most common cleaning steps, how to handle types, duplicates and missing values in pandas, how to separate rows that need human review and how to turn the script into a reliable, scheduled job.
Related reading: data entry outsourcing vs automation.
Why automate spreadsheet cleaning
Manual cleaning has predictable problems:
- It takes time every week or month
- Different people clean data differently
- Mistakes slip through, such as a date read in the wrong format
- There is no record of what was changed
- Knowledge sits with one person
A script makes cleaning consistent, fast and repeatable, and the code itself documents what happens to the data.
What is pandas data cleaning?
Using the open-source pandas Python library to fix tabular data, such as names, types, duplicates and missing values, in a repeatable script instead of by hand.
What pandas is
Pandas is an open-source Python library for working with tabular data. It reads files such as CSV and Excel into a DataFrame, a table with named columns, and provides functions for filtering, transforming, combining and writing data. It suits files from a few rows to many millions, depending on available memory.
Start by profiling the data
Before writing cleaning rules, look at the data:
- Column names and types as pandas reads them
- Counts of missing values per column
- Unique values in categorical columns, to spot variants such as "UK", "U.K." and "United Kingdom"
- Minimum and maximum values for numbers and dates
- Duplicate counts on key fields, such as email or customer ID
Profiling shows which problems actually exist, so you write rules for real issues rather than imagined ones.
How do you clean messy data in pandas step by step?
Most cleaning scripts include some combination of:
- Standardising column names, for example lowercase with underscores
- Trimming whitespace and fixing letter case
- Converting text to dates and numbers
- Mapping inconsistent categories to standard values
- Removing or merging duplicates
- Handling missing values
- Validating results against business rules
Keep each step simple and in a clear order, so problems are easy to trace.
How do I convert messy dates and numbers in pandas?
Exported spreadsheets often store dates and numbers as text, sometimes with mixed formats. Pandas provides to_datetime and to_numeric, and both accept errors="coerce", which turns values that cannot be parsed into missing values, NaT for dates and NaN for numbers, instead of stopping the script (pandas.to_datetime, pandas.to_numeric).
Coercing is useful, but it hides problems if you ignore the results. Always count and review values that became missing during conversion.
How do I remove duplicates in pandas and control which one is kept?
Duplicates need a clear rule. The drop_duplicates method removes repeated rows based on chosen columns, with a keep option that decides whether the first or last occurrence is retained; the default keeps the first (pandas.DataFrame.drop_duplicates). Sorting first, for example by date, lets you control which version survives.
Normalise the matching fields before checking for duplicates, such as lowercasing emails and trimming spaces, or near-identical rows will slip through.
Related reading: CRM deduplication.
Worked example: a pandas data cleaning script
The Python sketch below cleans a customer export: it standardises columns, trims text, normalises emails, converts types, keeps the latest row per email and separates rows that need review. It is a simplified sketch, not tested on real data; column names are illustrative.
# Simplified sketch, not tested on real data
import pandas as pd
df = pd.read_csv("customers_export.csv", dtype=str)
# Standardise column names and trim whitespace
df.columns = df.columns.str.strip().str.lower().str.replace(" ", "_")
for col in df.columns:
df[col] = df[col].str.strip()
# Normalise emails and drop rows without one
df["email"] = df["email"].str.lower()
df = df[df["email"].notna() & (df["email"] != "")]
# Convert types; unparseable values become NaT or NaN for review
df["signup_date"] = pd.to_datetime(df["signup_date"], errors="coerce")
df["annual_spend"] = pd.to_numeric(df["annual_spend"].str.replace(",", ""), errors="coerce")
# Keep the most recent row for each email
df = df.sort_values("signup_date").drop_duplicates(subset="email", keep="last")
# Separate rows that need human review
needs_review = df[df["signup_date"].isna() | df["annual_spend"].isna()]
clean = df.drop(needs_review.index)
clean.to_csv("customers_clean.csv", index=False)
needs_review.to_csv("customers_review.csv", index=False)
The script writes two files: clean rows ready for import, and rows with unparseable dates or amounts for a person to check. Nothing is silently discarded except rows without an email, which you might also choose to send for review.
Handle missing values thoughtfully
Missing values need business rules, not automatic deletion:
- Required fields: send the row for review if missing
- Optional fields: leave empty
- Fields with a safe default: fill it, and record that you did
- Calculated fields: recalculate from source columns
Avoid filling missing numbers with zero unless zero is genuinely correct, as it can distort totals and averages.
Standardise categories
Free-text categories, such as countries, industries or status values, often contain many variants. Build a mapping table from each variant to a standard value, apply it, and report any values not in the table so the mapping can be extended. Keeping the mapping in a separate file lets non-developers update it.
Validate before writing output
Before saving, check the cleaned data against rules:
- Row counts compared with the input, with differences explained
- No duplicates on key fields
- Required fields present
- Values within expected ranges
- Dates not in the future where that makes no sense
Fail the run, or at least alert someone, if checks do not pass.
Test the cleaning rules
Cleaning code changes data, so it deserves tests. Build a small sample file containing every problem you know about, such as extra spaces, mixed date formats, thousands separators, duplicate emails in different cases and blank required fields, together with the output you expect. Run the script against it whenever you change a rule. When a new problem appears in real data, add an example to the sample file before fixing the code, so the same problem is caught automatically next time.
Turn the script into a reliable job
To run cleaning automatically:
- Read input from a fixed location, such as a shared folder or an export from the source system's API
- Log each run with row counts, review counts and any errors
- Keep the input file for each run for a limited period, to allow reprocessing
- Schedule the script with a job scheduler or workflow tool
- Notify someone when review files contain rows
Our guide to Python automation scripts explains how to package, schedule and monitor scripts like this.
Related reading: AI automation solutions.
When should you replace a spreadsheet process with a full pipeline?
A single cleaning script suits one file and one destination. When you combine several sources, need history, or load data into a warehouse or several systems, a structured ETL pipeline with staging, transformations and tests is usually better. Our guide to ETL pipeline development covers how such pipelines are designed.
Related reading: Python invoice parsing for PDF and Excel.
Protect the data
Customer exports often contain personal data. Store input and output files securely, limit access, avoid emailing files around and delete old files according to your retention policy.
Frequently asked questions
What is pandas data cleaning?
Using the open-source pandas Python library to fix tabular data, such as names, types, duplicates and missing values, in a repeatable script instead of by hand.
How do I convert messy dates and numbers in pandas?
Use pd.to_datetime and pd.to_numeric with errors='coerce' so values that cannot be parsed become missing instead of stopping the script, then route those rows to review.
How do I control which duplicate pandas keeps?
Sort the data so the row you want comes first, for example by last updated date, then call drop_duplicates on the key columns with keep='first'.
Should cleaning scripts delete bad rows?
No. Write rows that fail validation to a separate rejects file with the reason, so a person can fix them and nothing disappears silently.
When should I move beyond a pandas script?
When several sources, schedules or downstream systems depend on the data, a monitored pipeline or direct integration is usually more reliable than a scheduled script.
Stop cleaning the same spreadsheet every week
We turn manual spreadsheet cleaning into tested, scheduled Python jobs with review files, validation and logging. See our custom app development services, or talk to us about data cleaning automation.
Related reading: data analytics and BI services.
About the Author
Manav Sharma is the founder of Insyrge and a CRM solutions architect. He works across Zoho, Salesforce, HubSpot, Odoo and more, designing the CRM setups, integrations and automations that keep sales and operations teams working from the same data.
Need Help Implementing This in Your Business?
Our certified Zoho consultants and automation experts can help you design and deploy custom workflows tailored to your operations.
Book Free Consultation