← All posts

Tutorial

ChatGPT CSV Cleanup: Fix Duplicates and Mixed Dates

Use ChatGPT CSV cleanup with explicit rules, review files and a tested Python example to catch duplicates, ambiguous dates and incorrect totals.

15 min read

Copy this article

Clipboard access is unavailable. Select the Markdown and copy it, then paste it into your chat.

ChatGPT CSV cleanup is most useful when you specify the rules before asking for a report. An assistant can help inspect a file and propose transformations, but you still need to check which records survived, which changed and which need a decision. A polished chart cannot tell you whether an ambiguous date was guessed or a conflicting order was silently removed.

This guide uses eight fictional sales records and a complete Python reference implementation. You can use the same rules as a ChatGPT prompt and compare its output with the reference files. The Python cleanup and edge cases were tested locally; a ChatGPT file-upload run was not performed for this example. No model accuracy or speed result is claimed.

Quick answer

Keep the original CSV, treat identifiers as text, define accepted date formats and remove only exact duplicate records. Quarantine all records sharing a conflicting identifier. Use decimal amounts and export a row-level change log. Then reconcile the input into cleaned, removed and review rows before creating a chart. In this example, eight input rows become three cleaned rows, one removed duplicate and four review rows. The cleaned total is 225.50, which is the total of the accepted subset rather than all eight input records.

Original diagram showing eight input rows partitioned into three cleaned, one removed and four review rows.

Prerequisites and complete download

Download the CSV cleanup source package and extract it into a new folder. It contains the original synthetic CSV, clean.py, test_clean.py, a README and the verified example outputs. Python 3.10 or newer is sufficient; the script uses the standard library and needs no package installation or API key.

For the optional ChatGPT workflow, use an account and interface that can inspect the file or accept pasted CSV text. Availability and account limits can change. The official ChatGPT use-case library is a starting point for practical workflows; it does not establish that a particular cleanup output is correct. If your interface cannot execute code or produce downloads, ask it for a script and run that script locally instead.

Keep this demonstration separate from a real customer export. All names and records here are invented. Before using any external service for your own data, follow the handling rules that apply to that dataset.

Step 1: inspect the input before transforming it

The supplied sales.csv contains these exact records:

order_id,order_date,category,amount
001,2026-10-01,Software,120.00
001,2026-10-01,Software,120.00
002,2026/10/02, software ,80.50
003,03/04/2026,Hardware,200.00
004,2026-10-03,Hardware,not-a-number
005,2026-10-04,Services,50.00
005,2026-10-04,Services,55.00
006,2026-10-05,SERVICES,25.00

The first two rows are identical. Order 002 has a supported year-first date and a category that needs normalization. Order 003 contains 03/04/2026: without a locale rule, that could mean March 4 or April 3. Order 004 has an invalid amount. Order 005 appears twice with different amounts. Order 006 has a valid record with uppercase category text.

These are different problems. Dropping every repeated ID would keep an arbitrary version of order 005. Parsing every date with one guessed locale would conceal uncertainty. Converting 001 to a number would destroy part of the identifier.

Open the file as text first. Record its column names, delimiter and row count. If a spreadsheet has already converted IDs or dates, return to the original export: a cleanup script cannot recover characters that were lost before it read the file.

Step 2: state the cleanup rules

Use a small specification that someone can check without knowing which model performed the transformation.

Field or condition Rule for this example
Order ID Preserve as text, trim surrounding whitespace and reject blank IDs
Date Accept YYYY-MM-DD and YYYY/MM/DD; output ISO dates
Other date formats Send to review; do not infer a locale
Category Trim and map software, hardware and services to the agreed labels
Amount Finite, nonnegative decimal value with at most two decimal places
Exact repeated record Keep the first valid record and log subsequent copies as removed
Same ID, different raw record Quarantine every record for that ID
Invalid record Preserve the original values in the review file with a reason

The conflicting-ID rule deliberately compares raw records. If two versions differ only in capitalization, this example still sends them to review. You could permit equivalence after normalization, but that is a different policy and should be specified and tested before implementation.

Likewise, this example rejects negative amounts because it models sales rather than refunds. A real export may need refunds, multiple currencies or more precision. Change the policy before changing the code. An assistant should not decide those business rules from a column name alone.

Step 3: ask ChatGPT for an auditable transformation

Upload the fictional file or paste its CSV text, then use this prompt:

Inspect the supplied sales CSV before making charts.

Use these rules:
- Keep order_id as text, including leading zeroes.
- Accept only YYYY-MM-DD and YYYY/MM/DD dates.
- Send ambiguous dates to review; do not infer date locale.
- Normalize category to Software, Hardware or Services.
- Use decimal amounts. Reject nonfinite, negative or sub-cent values.
- Remove only exact duplicates of valid records.
- If the same order_id has different raw records, quarantine all of them.
- Keep invalid records in review with their original values and a reason.

Return cleaned.csv, removed.csv, review.csv, a row-level change log
and a summary reconciling all input rows. Show the code used.
Report the cleaned subset total separately from review amounts.
Do not generate a chart until these checks are complete.

Inspect the proposed code before running it. Look for duplicate handling, date parsing and amount conversion specifically. If the assistant uses binary floating-point for totals, silently guesses a locale or keeps one conflicting record, ask for a corrected implementation. Do not accept a verbal assurance in place of the output files.

If you repeat the exercise, record the model displayed, date, prompt and any corrections. That creates a useful record of the particular run without turning a single small example into a general benchmark.

Step 4: run the reference implementation

From the extracted folder, run these commands. On Windows, py may be the available Python launcher; otherwise replace it with your Python executable.

py -3 clean.py
py -3 -m unittest -v

On macOS or Linux:

python3 clean.py
python3 -m unittest -v

The script reads sales.csv beside itself and writes only its output directory. It leaves the input file intact. The complete source is in the download; no omitted helper function or cloud service is needed.

The summary should be:

{
  "input_rows": 8,
  "cleaned_rows": 3,
  "removed_rows": 1,
  "review_rows": 4,
  "cleaned_total": "225.50"
}

The unit tests compare the known fixture with this expected result and check invalid dates, blank IDs, unknown categories, nonfinite amounts, negatives and sub-cent precision. They verify the cleanup policy implemented here. They do not verify a ChatGPT response until you compare that response’s files against the same expectations.

Original result diagram listing cleaned IDs 001, 002 and 006 and the accepted-subset total of 225.50.

Step 5: reconcile the files, not just the summary

cleaned.csv should contain orders 001, 002 and 006 with amounts 120.00, 80.50 and 25.00. Order 002’s date becomes 2026-10-02. Its category becomes Software, and order 006’s category becomes Services.

removed.csv should contain the second copy of order 001. Its source line is 3 because the header is line 1. review.csv should contain source lines 5, 6, 7 and 8: the ambiguous date, invalid amount and both versions of order 005. change-log.json should contain one decision for each input record.

The row partition is the first reconciliation:

8 input rows = 3 cleaned rows + 1 removed row + 4 review rows

The monetary total is a separate check. The accepted subset totals 225.50. You cannot describe that as the total sales in the original export because unresolved records remain. In particular, the two conflicting order 005 amounts cannot both count as separate sales, and neither can be selected without a decision.

If a result reports eight accounted-for rows but attaches the wrong source lines, it still fails. Check identities and reasons as well as counts. Counts can match while the wrong rows have been removed.

Step 6: resolve the review queue deliberately

Suppose the source owner confirms that order 003 uses day/month/year. Record that decision and change the policy to permit that format for the relevant source. Do not simply overwrite the review output: update a copy of the input or a separate corrections file, rerun the process and preserve the decision trail.

For order 005, ask which source record is authoritative. A later timestamp, signed correction or system-of-record lookup could settle it, but this CSV contains none of those. The assistant cannot infer the right amount from the two values alone.

For the invalid amount, return to the source. Replacing it with zero would change the financial meaning of the record and hide the error. A missing amount, zero amount and unparsable amount are distinct states.

Only after these decisions should you make the final report. If you chart the accepted subset while review is pending, label it as an accepted subset and disclose the unresolved record count.

Troubleshooting common cleanup failures

If Python is unavailable, install a supported Python release or use an existing environment. There are no package dependencies for this example. If the script reports unexpected columns or a malformed CSV row, check the header, delimiter and cell count instead of deleting the validation. Missing or extra cells stop the file before outputs are written; ordinary invalid field values go to review. Amounts accept unsigned decimal notation with at most two decimal places, so scientific notation and separators such as underscores are rejected. The example intentionally accepts one exact schema.

If IDs lose zeroes in your spreadsheet, import that column as text or inspect the output in a text editor. The Python script preserves 001; a later spreadsheet import can still reinterpret it.

If a date is quarantined that you consider obvious, check whether the rule actually permits its format. A known business convention belongs in the specification and tests. It should not live only in someone’s interpretation of a screenshot.

If the cleaned total differs, compare the three accepted IDs first. Then inspect normalization and duplicate decisions. Do not fix a total by rounding or adding a manual adjustment before finding the underlying discrepancy.

This small reference script is intended for the supplied schema. It does not handle every delimiter, currency, encoding or streaming-scale dataset. It provides an inspectable pattern: explicit rules, preserved input, visible exceptions and reproducible checks.

Make the handoff understandable to the next person

A clean file without a record of the cleanup decisions can create work for whoever uses it next. Include the input filename, the rule version, the output filenames and the unresolved review count. Describe whether the total covers accepted rows or the entire original file. In this example, 225.50 is the accepted subset total. Calling it the original sales total would hide the rows that still need review.

Keep the original export alongside the results, with access appropriate to its contents. If someone questions a changed value, they should be able to locate the original row and the rule that produced the change. A model’s explanation is useful context, but the reproducible transformation and its inputs are the evidence.

For a recurring workflow, decide what happens when the export format changes. A new column, a missing cell or an unexpected extra value should trigger a schema review before processing continues. The reference implementation stops on malformed row shapes instead of silently dropping cells. That means you repair the input format and rerun the process, rather than interpreting a partially written output as a complete result.

Extend the rules with one case at a time

Suppose a later export uses a currency symbol in the amount column. Do not broaden the parser just because one value looks obvious. First decide whether the symbol identifies a currency, whether more than one currency can occur and how that information belongs in the output. Then add a fixture representing the new accepted format and another representing a format that must remain invalid.

Dates need the same care. If an export includes an ambiguous date, a rule cannot recover information that was never supplied. Ask for the export’s documented date convention or route that row to review. Guessing based on neighbouring rows can produce a consistent looking file with incorrect dates.

After each rule change, run the existing tests as well as the new cases. Reconcile the row counts again and inspect which records moved between accepted, removed and review outputs. A smaller review queue is only an improvement if the resolved rows are correct. Keep the reason for each resolution visible so the next cleanup run can use the same decision deliberately.

FAQ

How do you clean a CSV file with ChatGPT?

Keep the original CSV and write the rules before you ask for a report: identifiers as text, accepted date formats, removal of exact duplicates only and quarantine for conflicting identifiers. Then upload the file or paste the CSV text with a prompt that states those rules and asks for cleaned, removed and review files plus a row-level change log. Inspect the proposed code before running it, and if your interface can’t execute code or produce downloads, ask for a script and run it locally.

How should you handle duplicate rows in a CSV cleanup?

Remove only exact duplicates of valid records. The first valid record is kept and later copies are logged as removed, so the second copy of order 001 is removed in this example. If the same ID appears with different raw records, as order 005 does with amounts 50.00 and 55.00, every record for that ID goes to review, because dropping every repeated ID would keep an arbitrary version.

How do you fix mixed or ambiguous dates in a CSV?

Accept only the formats your rules name, which here are YYYY-MM-DD and YYYY/MM/DD, and output ISO dates. Anything else goes to review without a guessed locale, since 03/04/2026 could mean March 4 or April 3. If the source owner confirms a convention such as day/month/year, record that decision, change the policy for that source and rerun on an updated copy of the input or a separate corrections file instead of overwriting the review output.

Why is the cleaned total 225.50 instead of the total of all rows?

The 225.50 figure covers only the accepted subset: orders 001, 002 and 006 at 120.00, 80.50 and 25.00. The eight input rows split into three cleaned rows, one removed duplicate and four review rows, and the review rows are still unresolved. Calling 225.50 the original sales total would hide them, so label it as an accepted subset and disclose the unresolved record count.

What are the limits of this ChatGPT CSV cleanup example?

The Python cleanup and edge cases were tested locally on eight fictional records, but a ChatGPT file-upload run was not performed for this example and no model accuracy or speed result is claimed. The reference script is intended for the supplied schema and doesn’t handle every delimiter, currency, encoding or streaming-scale dataset. Availability and account limits for ChatGPT can change, and before using any external service for your own data you should follow the handling rules that apply to that dataset.

Do you need an API key or extra packages to run the CSV cleanup script?

No. Python 3.10 or newer is sufficient, since the script uses the standard library and needs no package installation or API key. From the extracted folder, run py -3 clean.py and py -3 -m unittest -v on Windows, or python3 clean.py and python3 -m unittest -v on macOS or Linux. The script reads sales.csv beside itself, writes only its output directory and leaves the input file intact.

Where to go next

Use the AI model task-testing guide to compare repeated cleanup or classification runs under a fixed rubric. For machine-readable model responses, the JSON Schema guide explains another useful check. Valid structure and correct data are separate requirements; verify both before reporting a result.

  • #chatgpt
  • #data-cleaning
  • #python
  • #tutorials