Write a Data Cleaning Script for Messy Data
Generate a step-by-step Pandas data cleaning script with issue detection and before/after summaries.
This prompt generates a structured pandas cleaning script that detects each data-quality issue you describe, applies a fix, and prints a before/after summary, after backing up the original data. It's aimed at analysts and data engineers staring at a messy CSV who want a systematic, auditable cleaning pass rather than ad-hoc one-off edits. The detect/fix/summary loop is the key design choice — it makes every transformation visible and reversible, so you can trust what the script actually changed instead of crossing your fingers.
You are a data engineer. I have a messy dataset with these issues: [DESCRIBE ISSUES e.g. inconsistent date formats, duplicate rows, mixed casing in categorical columns, outliers in numeric fields]. Write a Python script using pandas to clean it systematically. For each cleaning step: (1) detect the issue, (2) apply the fix, (3) print a before/after summary. Preserve the original data in a backup copy before mutating. SAMPLE DATA: [PASTE SAMPLE ROWS OR SCHEMA]
What you can expect back
import pandas as pd
df = pd.read_csv("customers.csv")
df_backup = df.copy() # preserve original before any mutation
# 1. Duplicate rows
dupes = df.duplicated().sum()
print(f"[Duplicates] found: {dupes}")
df = df.drop_duplicates()
print(f"[Duplicates] rows after: {len(df)}")
# 2. Inconsistent date formats
print("[Dates] unique sample before:", df['signup_date'].unique()[:5])
df['signup_date'] = pd.to_datetime(df['signup_date'], errors='coerce')
print(f"[Dates] unparseable (now NaT): {df['signup_date'].isna().sum()}")
# 3. Mixed casing in country
print("[Country] before:", df['country'].unique())
df['country'] = df['country'].str.strip().str.upper()
print("[Country] after:", df['country'].unique())
# 4. Negative ages
bad_age = (df['age'] < 0).sum()
print(f"[Age] negative values: {bad_age}")
df.loc[df['age'] < 0, 'age'] = pd.NA
print(f"[Age] nulls after fix: {df['age'].isna().sum()}")
print("\nCleaning complete. Original preserved in df_backup.")Illustrative example — your results will vary by tool and inputs.
Get sharper results
- 01Paste real (anonymized) sample rows including the actual messy values — the model writes far better detection logic when it can see the exact formats it's dealing with.
- 02Tell it how to handle ambiguous fixes: should negative ages become null, zero, or be dropped? If you don't decide, the script picks for you and may not match your intent.
- 03Ask it to write the cleaned result to a new file rather than overwriting the source, so a buggy fix can never destroy your raw data.
- 04Request a final data-quality report (null counts, row count, dtype summary) at the end so you can confirm the cleaning did what you expected.
- 05For large files, ask it to add chunked reading or dtype specification so the script doesn't blow up your memory on load.
Adapt it for your case
Ask for the logic wrapped in a parameterized clean_data(df) function with the column names as arguments, so you can rerun it on future batches.
Swap 'using pandas' for 'using Polars' to get a faster, lazy-evaluation version for large datasets.
Drop the fixes and ask only for a diagnostic report that detects and counts every issue without mutating anything.
Common questions
Will it handle a dataset it can't see?
It writes generic logic from your description, but accuracy jumps when you paste real sample rows; without them, expect to adjust column names and edge-case handling yourself.
Is the script safe to run on my only copy?
It backs up to a copy in memory, but you should still run it against a duplicate file, not your sole raw source, in case a fix behaves unexpectedly on full data.
What if a fix removes valid data?
That's why each step prints a before/after summary — review those counts, and if a step over-corrects, ask the model to loosen that specific detection rule.
You may also need
Write a SQL Query From a Business Question
Translate a business question into a clean, commented SQL query against your schema.
Analyze a Dataset With Pandas Step-by-Step
Generate step-by-step Pandas EDA code covering nulls, outliers, and a business question.
Use SQL Window Functions for Advanced Analytics
Generate advanced SQL window function queries with explanations and performance notes.
Turn a Plain-English Question Into a SQL Query
Converts a natural-language business question into a correct, commented SQL query grounded in your real schema.