Sub topic 6
Auditing Missing Values, Duplicates, and Odd Values
This chapter assumes you can use pandas DataFrames and run cells in Jupyter. It also assumes you have loaded df_raw from data/raw/orders.csv and taken a first look at its shape and types. The goal here is to find out what is wrong with the data before you change anything.
Audit first, clean later
An audit is a read-only pass that lists problems. Cleaning is a separate step that fixes them. Keeping the two apart gives you a record of what the data looked like, and each cleaning rule can point back to a problem you saw.
Run the audit on df_raw, never on a cleaned copy. The audit output is evidence for the next chapter.
A reusable quality report
A small function summarizes each column in one table. Putting it in a function lets you run it again after cleaning and compare the two reports.
import pandas as pd
def quality_report(df):
return pd.DataFrame({
"dtype": df.dtypes.astype(str),
"missing": df.isna().sum(),
"missing_pct": (df.isna().mean() * 100).round(1),
"n_unique": df.nunique(),
})
display(quality_report(df_raw))
print("duplicate rows:", df_raw.duplicated().sum())display is available in Jupyter and IPython. In a plain Python script, use print instead.
Read the table column by column:
- dtype: a numeric column stored as
objectoften means text such as"N/A"or"$12.50"is mixed in. - missing and missing_pct: a column with 0.2% missing is a different problem from one with 40% missing.
- n_unique: an ID column with fewer unique values than rows suggests duplicates. A category column with many unique values may contain spelling variants.
Missing values
Counting missing values is not enough. Ask why they are missing. A blank amount might mean a cancelled order, a failed export, or a real zero. Each reading calls for a different fix, so look at the affected rows before choosing one.
missing_amount = df_raw[df_raw["amount"].isna()]
print(len(missing_amount))
missing_amount.head()Check whether the missing rows share a pattern, such as one region or one date range. A pattern suggests a systematic cause. Scattered gaps look more like random loss.
Duplicates
df_raw.duplicated() flags rows that match an earlier row in every column. This misses a second kind of duplicate: the same order entered twice with a small difference, such as a different timestamp. If the data has an identifier, check it directly.
print("fully duplicated rows:", df_raw.duplicated().sum())
print("repeated order_id:", df_raw["order_id"].duplicated().sum())This assumes the file has an order_id column. If the second number is larger than the first, some orders appear more than once with differing details. Decide which copy is correct before you drop anything.
Odd values
Odd values are present and typed correctly, but they do not make sense. Look for these:
- Negative amounts where only positive ones are possible.
- Dates in the future or far in the past.
- Category labels that differ only in case or spacing, such as
"North"and" north ".
print("negative amounts:", (df_raw["amount"] < 0).sum())
print("date range:", df_raw["order_date"].min(), "to", df_raw["order_date"].max())
print(df_raw["region"].value_counts(dropna=False))Passing dropna=False keeps missing labels visible in the counts. The describe() output from your first look also helps. A maximum far above the 75th percentile is worth a closer look.
Log what you find
Write each problem in a markdown cell with its count. For example: "amount missing in 3.1% of rows; 12 negative amounts; region has mixed case." This list becomes the specification for your cleaning function.
Exercise
Why run the quality report on df_raw and not on a cleaned copy, and what extra check does df.duplicated() miss when the data has an order ID?
Show answer
The audit must describe the data as it arrived, so the issue list reflects real problems and later cleaning rules can be traced to them. duplicated() only catches rows identical in every column, so it misses repeated order_id values whose other columns differ. Check df["order_id"].duplicated() as well.