← All Python interview questions
Produce a data-quality audit for a raw extract
HardPandas — CleaningAccentureEY
Before any analysis, you run a standard audit on a raw extract.
Write audit(df) that returns a dict with rows, duplicate_rows, null_counts (a dict of column to null count, only for columns that actually have nulls), constant_columns (a sorted list of columns with a single distinct non-null value) and negative_amounts (the count of rows where amount is below zero).
This fixture runs before your code on every Run and Submit, so the data is always clean.
import pandas as pd
import numpy as np
raw = pd.DataFrame({
"order_id": [1, 2, 3, 4, 4],
"amount": [100.0, -50.0, np.nan, 300.0, 300.0],
"country": ["IN", "IN", "IN", "IN", "IN"],
"channel": ["web", "app", None, "web", "web"],
})- Step 1: `df.isna().sum()` gives per-column null counts in one vectorised pass; filtering to non-zero entries keeps the report readable.
- Step 2: `df.duplicated().sum()` counts fully repeated rows — the cheapest signal that an extract was double-loaded.
- Step 3: `nunique(dropna=True) == 1` finds constant columns, which carry no information and often reveal a broken filter upstream.
- Step 4: `(df['amount'] < 0).sum()` is a domain rule, not a generic check — every audit should include a couple of these.
- Step 5: Casting to plain `int` makes the dict JSON-serialisable so it can be logged or stored with the run.
def audit(df):
nulls = df.isna().sum()
null_counts = {col: int(n) for col, n in nulls.items() if n > 0}
constant_columns = sorted(col for col in df.columns if df[col].nunique(dropna=True) == 1)
return {
"rows": int(len(df)),
"duplicate_rows": int(df.duplicated().sum()),
"null_counts": null_counts,
"constant_columns": constant_columns,
"negative_amounts": int((df["amount"] < 0).sum()),
}Learn the concept first
Stuck on Pandas — Cleaning? These free Python lessons teach it step by step.
More Pandas — Cleaning questions
Practising alone only goes so far — our live cohorts pair Python and Pandas depth with dashboards, agentic analytics projects and mock interviews.
Explore the program