Anyone can write df.to_csv('cleaned.csv'). The hard part is answering the question the person paying for it will actually ask:

"Did you change my numbers?"

If your answer is "I looked at it," you don't have an answer. You have a hope. I've been doing fixed-price data cleanup long enough to have learned this the expensive way, so every job I return ships with a proof — and building that proof changed how I clean files in the first place.

Here is the method, including the parts that don't work.

Why the obvious proof is worthless

The obvious move is to hash the file before and after:

import hashlib def sha(p): return hashlib.sha256(open(p,'rb').read()).hexdigest() print(sha('messy.csv') == sha('cleaned.csv')) # False, every time 

This is False for a correct cleanup, because the bytes change by definition: 1,234.50 becomes 1234.50, bob becomes bob, a stray \ufeff BOM disappears, 2024-1-5 becomes 2024-01-05. The file is supposed to differ. Byte equality proves nothing about numeric equality.

The second obvious move — compare the parsed frames with df1.equals(df2) — is worse, because it fails for benign reasons too (column order, dtype drift, NaN vs None) and it gives you one bit of information: pass or fail. When it says False you still have to go find out which of 50,000 cells moved.

The proof that actually holds: invariants, not bytes

The insight is that you don't need to prove the files are the same. You need to prove the numbers are the same. So you define a set of arithmetic invariants that a number-preserving transformation cannot change, and check every one of them, twice — once before, once after.

For each numeric column:

  • row count (non-null count, not total rows)
  • sum, as Decimal, rounded to a fixed scale
  • min / max
  • count of distinct values
  • count of values that look numeric but didn't parse — the one people skip

For each text column:

  • count of distinct values
  • count of empty/whitespace-only cells
  • count of cells whose trimmed value differs from the raw value (if you're about to trim, this is your diff)

For the whole file:

  • count of rows dropped and count of rows split (multi-line quoted fields parsed wrong is the classic bug)
  • sum of every numeric column, as one Decimal total

That last one is the headline number. If the total gross amount across the file is identical to the cent before and after, you've proved you didn't invent or destroy money. If it isn't, nothing else matters.

from decimal import Decimal, ROUND_HALF_UP def money(x): return Decimal(str(x)).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP) def fingerprint(df, money_cols): fp = { 'rows': len(df), 'money_total': sum((money(v) for c in money_cols for v in df[c].dropna()), Decimal('0')), } for c in money_cols: s = df[c].dropna().map(money) fp[f'{c}.sum'] = sum(s, Decimal('0')) fp[f'{c}.min'] = s.min() if len(s) else None fp[f'{c}.max'] = s.max() if len(s) else None fp[f'{c}.distinct'] = s.nunique() return fp 

Run fingerprint on the raw parse and on the cleaned frame, then diff the two dicts. Any key that moved is a bug you introduced. That's the whole proof, and it takes about 20 lines.

Three traps that make this proof lie

1. float64 rounding. If your money column is float, 0.1 + 0.2 != 0.3 and your "identical" totals will differ in the 15th decimal, so you'll either chase ghosts or start rounding so aggressively you hide real errors. Parse money as string → Decimal at read time:

df = pd.read_csv(path, dtype=str, keep_default_na=False) df['amount'] = df['amount'].map(lambda s: Decimal(s.replace(',', '').replace('$','')) if s.strip() else None) 

Reading everything as str first also stops pandas from silently turning 007 into 7 and 1-2 into a date.

2. Silent drop of unparseable values. pd.to_numeric(errors='coerce') is the single most common way cleanups lose data. Fifteen rows say N/A, they become NaN, you dropna(), and the sum quietly changes by 15 rows' worth. Always count the coerced-to-NaN values before dropping, and report that count:

before = df['amount'].notna().sum() df['amount_num'] = pd.to_numeric(df['amount'], errors='coerce') lost = (df['amount'].notna() & df['amount_num'].isna()).sum() assert lost == 0 or lost == expected_bad_rows, f'{lost} values failed to parse' 

3. Encoding that eats characters. latin-1 never raises — it happily decodes UTF-8 bytes into mojibake. A name column can come back with the right row count, the right sums, and José turned into José. Text invariants won't catch it. The only reliable test is to check the file's BOM and try utf-8 strictly first, and only fall back to a declared encoding:

for enc in ('utf-8-sig', 'utf-8', 'cp1252', 'latin-1'): try: df = pd.read_csv(path, encoding=enc, dtype=str, keep_default_na=False) break except UnicodeDecodeError: continue 

If it only parses at latin-1, say so in the delivery note. That's information the client needs.

Ship the change log with the file

The invariant check tells you the numbers survived. It doesn't tell the client. So every cleaned file goes out with a second file, a per-rule log:

column,rule,rows_affected,example_before,example_after,reversible amount,strip currency symbol,1204,"$1,234.50","1234.50",yes amount,parse as Decimal,1204,"1234.50","1234.50",yes name,trim whitespace,88," bob ","bob",yes date,normalize to ISO,1204,"2024-1-5","2024-01-05",yes rows,drop fully-empty rows,3,"","",yes amount,set unparseable to blank (reported),2,"N/A","",no 

Two things in that table matter more than the rest. reversible says whether the original value can be reconstructed — and the honest answer is no for anything you blanked out. And the last column forces you to admit the one row that isn't a transformation but a deletion.

That table is also what makes the job cheap to do a second time. Next month, the same client sends the same export with the same six problems, and you already have the rules written down.

The uncomfortable part

Almost every "cleaned" spreadsheet someone sends you is wrong in a way nobody checked. The value of the invariants isn't that they're clever — they're trivial. It's that they're cheap enough to run every time, so they actually get run every time, including on the file you're about to send at 11pm.

If you build this into a script rather than your head, the whole cleanup becomes a two-command job: run the lint, run the invariants, ship the file plus the log.

If you'd rather hand the file over: I run exactly this process on CSV/Excel cleanups as a fixed-price job — up to 1,000 rows for $25, 2-day turnaround, cleaned file plus the per-rule change log above (Fiverr gig).