Clean data and retain evidence

Convert values deliberately, recover amounts when inputs support it, and record exclusions.

On this page

Convert and recover amounts

Keep the recorded total if readable. If it is missing, compute quantity × unit price only when both are available. Flag totals you recovered and keep unresolved amounts missing. On real data, first check whether discounts, tax, or refunds invalidate that formula.

import pandas as pd

raw = pd.read_csv("store_sales.csv", dtype="string", keep_default_na=False)
clean = raw.drop_duplicates().copy()
quantity = pd.to_numeric(clean["Quantity"], errors="coerce")
price = pd.to_numeric(clean["Price Per Unit"], errors="coerce")
recorded = pd.to_numeric(clean["Total Spent"], errors="coerce")
computed = quantity * price
clean["sales"] = recorded.fillna(computed)
clean["is_recovered"] = recorded.isna() & computed.notna()
print(clean["is_recovered"].sum(), clean["sales"].isna().sum())
Example output
1 1

Validate each rule against the raw data

Measure row counts and missing values before and after each change. Keep the original amount alongside a recovered amount when you need to explain differences. If you drop rows, record the count and reason.

Official references