pandas-data-cleaning
Skill11 starsChanged 6 months agoMITView on GitHub
Cleans messy tabular datasets in pandas end-to-end — fixing dtypes, parsing dates and numbers, standardizing text, handling missing values, removing duplicates, detecting and treating outliers, and reshaping wide/long into tidy data. Use this skill when the user asks to "clean this CSV/Excel/dataframe", "fix data types", "handle missing values / NaNs", "remove duplicates", "deal with outliers", "standardize column names or categories", "parse dates", "melt/pivot/reshape", or to build a reproducible cleaning pipeline before analysis or modeling.
In this repository74 other files it gives its agents.
- 0-templateSkill
- ab-test-analyzerSkill
- adr-authorSkill
- api-designerSkill
- api-docs-writerSkill
- aws-cost-optimizerSkill
- bash-script-hardeningSkill
- blog-post-writerSkill
- book-summarizerSkill
- changelog-keeperSkill
- chart-chooserSkill
- code-reviewerSkill
- competitive-analysisSkill
- concept-explainerSkill
- cover-letter-writerSkill
- cron-schedulerSkill
- data-pipeline-architectSkill
- debug-detectiveSkill
- decision-matrixSkill
- dependency-upgraderSkill
- dockerfile-proSkill
- email-composerSkill
- error-handling-patternsSkill
- event-plannerSkill
- flashcard-generatorSkill
- gdpr-data-mapperSkill
- gift-advisorSkill
- github-actions-builderSkill
- git-workflowSkill
- habit-builderSkill
- incident-postmortemSkill
- interview-prepSkill
- job-description-writerSkill
- json-schema-authorSkill
- kubernetes-manifest-authorSkill
- language-tutorSkill
- meal-plan-builderSkill
- meeting-summarizerSkill
- mermaid-diagram-builderSkill
- nginx-config-proSkill
- observability-setupSkill
- okr-writerSkill
- performance-profilerSkill
- personal-budget-plannerSkill
- presentation-builderSkill
- project-plannerSkill
- prompt-engineerSkill
- proofreaderSkill
- public-speaking-coachSkill
- rag-pipeline-designerSkill
- readme-generatorSkill
- refactoring-guideSkill
- regex-architectSkill
- release-notes-writerSkill
- resume-writerSkill
- salary-negotiatorSkill
- secrets-managerSkill
- secure-code-reviewSkill
- secure-password-policySkill
- security-auditorSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
7 rules. About 0 are common, 3 are distinctive, and 4 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈3
Distinctive · estimate
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 15
Treat cleaning as a pipeline that produces (1) the cleaned DataFrame and (2) a record of decisions.
- Very specific: line 19
Profile the raw data: Before changing anything, understand it.
- Very specific: line 31
Detect & treat outliers: Use IQR fences or z-scores to flag, then decide: keep (legitimate extreme), cap/winsorize, transform (log), or …
- Very specific: line 33
Reshape to tidy: Use melt to go wide→long, pivot/pivot_table for long→wide, and str.split/explode to split packed columns.
- Distinctive: line 59
Use robust methods (IQR, median) over mean/std on skewed data, since mean/std are themselves dragged by outliers.
- Distinctive: line 86
Keep the raw file immutable: Always read raw, write cleaned to a new path.
- Distinctive: line 92
Auto-deleting outliers: before checking whether they're legitimate, throwing away the most interesting rows.
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
pandas Data Cleaning
Overview
Keywords: pandas, data cleaning, dtypes, missing values, NaN, imputation, duplicates, outliers, IQR, z-score, tidy data, melt, pivot, normalize, standardize, parse dates, categorical, data quality, ETL preprocessing.
This skill turns a messy DataFrame into a tidy, correctly-typed, analysis-ready dataset using a repeatable, auditable workflow. The core principle: profile first, decide explicitly, transform with logging, validate after. Never mutate data silently — every fill, drop, or cast should be a deliberate, documented choice you can defend.
Treat cleaning as a pipeline that produces (1) the cleaned DataFrame and (2) a record of decisions. Prefer chained, non-mutating transforms (df.assign(...), .pipe(...)) over scattered in-place edits so the pipeline is reproducible top-to-bottom.
Workflow
- Profile the raw data. Before changing anything, understand it. Run
scripts/profile_data.py <path>(or replicate inline) to get shape, dtypes, per-column null counts/percentages, unique counts, sample values, and candidate problems (mixed types, high-cardinality strings, numeric-looking objects, constant columns). Seereferences/cleaning-checklist.md. - Fix structure. Standardize column names (snake_case, strip whitespace, dedupe). Set/verify the index. Drop fully-empty rows/columns and constant columns that carry no signal. Confirm one observation per row, one variable per column (tidy form). If not tidy, defer reshape to step 8.
- Coerce dtypes. Convert numeric-looking strings (
"1,234","$5.00","12%") to numbers, parse dates with explicit formats, cast low-cardinality strings tocategory, and use nullable dtypes (Int64,boolean,string) where missing values must coexist with non-float types. Seereferences/dtype-conversion.md. - Standardize text & categories. Trim whitespace, normalize case, collapse synonyms ("USA"/"U.S.A."/"United States"), fix encoding artifacts, and map free-text categories to a controlled vocabulary.
- Handle missing values. First distinguish disguised missing (
"NA","-","unknown",-999, empty string) from real values and convert them toNaN/pd.NA. Then choose a strategy per column — drop, constant fill, statistical impute (mean/median/mode), forward/backfill for time series, or model-based — and document why. Use the decision framework below. - Remove duplicates. Detect exact duplicates and key-based duplicates (
subset=[...]). Decide keep policy (first/last/aggregate). Watch for near-duplicates from inconsistent text (handle those in step 4 first). - Detect & treat outliers. Use IQR fences or z-scores to flag, then decide: keep (legitimate extreme), cap/winsorize, transform (log), or remove (data error). Never delete outliers reflexively. See
references/outliers-and-reshaping.md. - Reshape to tidy. Use
meltto go wide→long,pivot/pivot_tablefor long→wide, andstr.split/explodeto split packed columns. Confirm the result satisfies the three tidy rules. - Validate. Re-profile. Assert invariants: expected row count range, no unexpected nulls in required columns, dtypes correct, key uniqueness, value ranges/domains. Fail loudly if violated. See the validation section in
references/cleaning-checklist.md. - Persist the recipe. Capture the ordered transforms in a single function/notebook cell so the same raw input always yields the same clean output. Use
templates/cleaning_report.mdto summarize what was done and why.
Missing-Value Decision Framework
| Situation | Recommended strategy |
| Column >50–60% missing, not critical | Drop the column |
| A few rows missing in a required key/target | Drop those rows |
| Numeric, missing-at-random, skewed | Impute median |
| Numeric, roughly symmetric | Impute mean (or median for robustness) |
| Categorical | Impute mode, or add explicit "Missing" category |
| Time series / ordered | ffill/bfill, or interpolate (.interpolate()) |
| Missingness is itself informative | Keep NaN + add boolean was_missing flag |
| Need non-float ints with NaN | Cast to nullable Int64, don't fill |
Rule of thumb: imputing changes the distribution. For modeling, prefer adding a missingness indicator alongside the imputed value so the model can learn from "was missing."
Outlier Decision Framework
- Flag, don't auto-delete. Compute IQR fences (
Q1 - 1.5*IQR,Q3 + 1.5*IQR) or |z| > 3. - Investigate. Is it a data-entry error (age = 999), a unit mistake (cm vs m), or a real rare event?
- Treat by cause: error → fix or remove; legitimate extreme but distorting → winsorize/cap at the fence or log-transform; legitimate and meaningful → keep.
- Use robust methods (IQR, median) over mean/std on skewed data, since mean/std are themselves dragged by outliers.
Worked Example (condensed)
Given a column price of strings like "$1,299.00", "N/A", "":
df["price"] = (
df["price"]
.replace({"N/A": pd.NA, "": pd.NA})
.str.replace(r"[$,]", "", regex=True)
.pipe(pd.to_numeric, errors="coerce") # bad parses -> NaN
)
df["price"] = df["price"].fillna(df["price"].median()) # documented: skewedSee examples/clean_messy_sales.md for a full raw→clean walkthrough with a 12-column messy sales file, and run scripts/clean_pipeline.py --help for a configurable end-to-end cleaner.
Best Practices
- Profile before and after. You cannot clean what you have not measured, and you cannot trust a clean you did not verify.
- Prefer non-mutating chains. Build the cleaned frame with
.assign/.pipeso the whole recipe is one readable, rerunnable block. Avoid sprinklinginplace=True. - Coerce with
errors="coerce", then inspect the new NaNs — they reveal unparseable values you'd otherwise miss. - Use explicit date formats (
pd.to_datetime(s, format="%Y-%m-%d")) to avoid silent misparsing of ambiguous01/02/03. - Use nullable dtypes (
Int64,boolean,string) instead of forcing floats just to hold NaN. - Document every destructive choice (drop/fill/cap) in the cleaning report so reviewers can challenge it.
- Validate with assertions at the end; treat a cleaning script that produces silently-wrong data as a bug.
- Keep the raw file immutable. Always read raw, write cleaned to a new path.
Common Pitfalls
- Disguised missing values (
"unknown",-999,"-", whitespace) left as real data, poisoning means and joins. Always normalize these to NaN first. inplace=Truechaining bugs and accidentalSettingWithCopyWarningfrom chained indexing — use.locand reassignment.- Auto-deleting outliers before checking whether they're legitimate, throwing away the most interesting rows.
- Mean-imputing skewed columns, dragging the central tendency and shrinking variance.
float64columns full of1.0/2.0because NaN forced float — cast toInt64after cleaning.- Ambiguous date parsing (
dayfirstvs default) silently swapping day/month. - Dropping duplicates before standardizing text, so
"Acme "and"acme"survive as distinct. - Pivot collisions — using
pivotwhen index/column pairs aren't unique; usepivot_tablewith an explicitaggfunc. - No post-clean validation, shipping a dataset whose row count or domain quietly broke.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

