TExcelTAD

Dataset Inspection

You have a new dataset. Before computing a single statistic or training anything, spend ten minutes simply looking at it. Inspection answers the basic questions: how big is it, what does one row mean, what type is each column, does each column contain what its name promises? Most serious data problems are visible at this stage to anyone who looks, and invisible to anyone who goes straight to model.fit.

This section of the course uses a small invented dataset of loan applications with realistic problems built in. Each lesson rebuilds it in a few lines, so every example runs on its own.

A first look

python · try it yourself
import pandas as pd

loans = pd.DataFrame({
    "application_id": ["A-001", "A-002", "A-003", "A-004", "A-005", "A-006", "A-007", "A-008"],
    "applied_on": ["2026-01-05", "2026-01-07", "07/01/2026", "2026-01-09", "2026-01-12", "2026-01-12", "2026-01-15", "2026-01-16"],
    "age": [34, 51, 29, -1, 45, 45, 38, 62],
    "income": ["52000", "61,500", "38000", "47000", "", "", "72000", "39000"],
    "country": ["VN", "VN", "vn ", "DE", "DE", "DE", "VN", "IN"],
    "product": ["loan"] * 8,
    "approved": [1, 0, 1, 1, 0, 0, 1, 0],
})
print("shape:", loans.shape)
print(loans.head(3))
print()
loans.info()
shape: (8, 7)
  application_id  applied_on  age  income country product  approved
0          A-001  2026-01-05   34   52000      VN    loan         1
1          A-002  2026-01-07   51  61,500      VN    loan         0
2          A-003  07/01/2026   29   38000     vn     loan         1

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 8 entries, 0 to 7
Data columns (total 7 columns):
 #   Column          Non-Null Count  Dtype
---  ------          --------------  -----
 0   application_id  8 non-null      object
 1   applied_on      8 non-null      object
 2   age             8 non-null      int64
 3   income          8 non-null      object
 4   country         8 non-null      object
 5   product         8 non-null      object
 6   approved        8 non-null      int64
dtypes: int64(2), object(5)
memory usage: 360.0+ bytes

info() lists every column with its count of non-null values and its dtype, plus the memory used. Three things already stand out: applied_on and income are object (text) although they should be a date and a number, and every column reports 8 non-null values, although income has empty strings. An empty string is not NaN, so pandas does not count it as missing.

Looking at every column

describe(include="all") summarises all columns at once; for text columns it gives the number of distinct values (unique), the most frequent value (top) and how often it appears (freq):

python · try it yourself
import pandas as pd

loans = pd.DataFrame({
    "application_id": ["A-001", "A-002", "A-003", "A-004", "A-005", "A-006", "A-007", "A-008"],
    "applied_on": ["2026-01-05", "2026-01-07", "07/01/2026", "2026-01-09", "2026-01-12", "2026-01-12", "2026-01-15", "2026-01-16"],
    "age": [34, 51, 29, -1, 45, 45, 38, 62],
    "income": ["52000", "61,500", "38000", "47000", "", "", "72000", "39000"],
    "country": ["VN", "VN", "vn ", "DE", "DE", "DE", "VN", "IN"],
    "product": ["loan"] * 8,
    "approved": [1, 0, 1, 1, 0, 0, 1, 0],
})
print(loans.describe(include="all").T[["count", "unique", "top", "freq", "min", "max"]])
               count unique         top freq  min   max
application_id     8      8       A-001    1  NaN   NaN
applied_on         8      7  2026-01-12    2  NaN   NaN
age              8.0    NaN         NaN  NaN -1.0  62.0
income             8      7                2  NaN   NaN
country            8      4          VN    3  NaN   NaN
product            8      1        loan    8  NaN   NaN
approved         8.0    NaN         NaN  NaN  0.0   1.0

Reading down the table: product has a single value (a constant column carries no information and can be dropped), age has a minimum of −1 (a placeholder for "unknown", a classic trick of older systems), country has 4 distinct values where 3 countries exist.

A column profile from scratch

describe mixes numbers and text awkwardly. A small profiling function gives one line per column with the facts you need most. Writing it yourself is a good exercise, and it is the first building block of the quality report at the end of this section:

python · try it yourself
import pandas as pd

loans = pd.DataFrame({
    "application_id": ["A-001", "A-002", "A-003", "A-004", "A-005", "A-006", "A-007", "A-008"],
    "applied_on": ["2026-01-05", "2026-01-07", "07/01/2026", "2026-01-09", "2026-01-12", "2026-01-12", "2026-01-15", "2026-01-16"],
    "age": [34, 51, 29, -1, 45, 45, 38, 62],
    "income": ["52000", "61,500", "38000", "47000", "", "", "72000", "39000"],
    "country": ["VN", "VN", "vn ", "DE", "DE", "DE", "VN", "IN"],
    "product": ["loan"] * 8,
    "approved": [1, 0, 1, 1, 0, 0, 1, 0],
})

def profile(df):
    rows = []
    for col in df.columns:
        s = df[col]
        text = s.astype(str)
        rows.append({
            "column": col,
            "dtype": str(s.dtype),
            "missing": int(s.isna().sum()),
            "empty_str": int((text.str.strip() == "").sum()),
            "distinct": int(s.nunique()),
            "numeric_like": round(pd.to_numeric(s, errors="coerce").notna().mean(), 2),
            "examples": ", ".join(text.drop_duplicates().head(3)),
        })
    return pd.DataFrame(rows).set_index("column")

print(profile(loans).to_string())
                 dtype  missing  empty_str  distinct  numeric_like                            examples
column
application_id  object        0          0         8          0.00                 A-001, A-002, A-003
applied_on      object        0          0         7          0.00  2026-01-05, 2026-01-07, 07/01/2026
age              int64        0          0         7          1.00                          34, 51, 29
income          object        0          2         7          0.62                52000, 61,500, 38000
country         object        0          0         4          0.00                         VN, vn , DE
product         object        0          0         1          0.00                                loan
approved         int64        0          0         2          1.00                                1, 0

The numeric_like column is the share of values that convert to a number. For income it is below 1: the values with a thousands comma and the empty strings fail to convert, so the column needs cleaning before it can be used as a number. For application_id it is 0, as it should be for an identifier.

Checking what a row means

The most important question about a dataset is what one row is. Here a row should be one application, so application_id should be unique and no two rows should describe the same application:

python · try it yourself
import pandas as pd

loans = pd.DataFrame({
    "application_id": ["A-001", "A-002", "A-003", "A-004", "A-005", "A-006", "A-007", "A-008"],
    "applied_on": ["2026-01-05", "2026-01-07", "07/01/2026", "2026-01-09", "2026-01-12", "2026-01-12", "2026-01-15", "2026-01-16"],
    "age": [34, 51, 29, -1, 45, 45, 38, 62],
    "income": ["52000", "61,500", "38000", "47000", "", "", "72000", "39000"],
    "country": ["VN", "VN", "vn ", "DE", "DE", "DE", "VN", "IN"],
    "approved": [1, 0, 1, 1, 0, 0, 1, 0],
})
print("application_id unique:", loans["application_id"].is_unique)
same_content = loans.drop(columns="application_id").duplicated(keep=False)
print("rows identical apart from the id:")
print(loans[same_content])
application_id unique: True
rows identical apart from the id:
  application_id  applied_on  age income country  approved
4          A-005  2026-01-12   45             DE         0
5          A-006  2026-01-12   45             DE         0

The ids are unique, but A-005 and A-006 are identical in every other column: probably one application submitted twice, which got two ids. Whether that is a duplicate or two real applications is a question for the data owner (Duplicate Detection).

A real dataset

The same routine on scikit-learn's breast cancer data, a clean, well-documented dataset, looks very different:

python · try it yourself
import pandas as pd
from sklearn.datasets import load_breast_cancer

df = load_breast_cancer(as_frame=True).frame
print("shape:", df.shape)
print("dtypes:", df.dtypes.value_counts().to_dict())
print("missing values in total:", int(df.isna().sum().sum()))
print("constant columns:", [c for c in df.columns if df[c].nunique() == 1])
print("duplicate rows:", int(df.duplicated().sum()))
print("target values:", df["target"].value_counts().sort_index().to_dict())
shape: (569, 31)
dtypes: {dtype('float64'): 30, dtype('int32'): 1}
missing values in total: 0
constant columns: []
duplicate rows: 0
target values: {0: 212, 1: 357}

No missing values, no duplicates, no constant columns: this is what a curated dataset looks like. Real project data rarely does, and that difference is why the rest of this section exists.

An inspection checklist

  1. Shape: rows and columns as expected? Compare with the source's documentation or row counts.
  2. Grain: what is one row? Is the key unique?
  3. Types: does each column's dtype match its meaning (dates, numbers, categories, ids)?
  4. Missing and placeholders: NaN, empty strings, -1, 999, "unknown", "N/A".
  5. Distinct values: constant columns, categories with spelling variants, ids that repeat.
  6. Ranges: minimum and maximum of every number; earliest and latest date.
  7. Samples: read 10–20 random rows in full, not just the first five. Files are often sorted, and the first rows are the oldest or the cleanest.

Pitfalls

  • Looking only at head(): sorted data hides its problems further down. Use sample().
  • Trusting isna() alone: empty strings and placeholder values are missing values in disguise.
  • Fixing while inspecting: write down what you find first; fix later, in code, in one place (Data Preparation).

Practice

  1. Which line finds placeholder values like -1, 999 or 9999 in all numeric columns at once?
Answer

df.select_dtypes("number").isin([-1, 999, 9999]).sum() counts them per column. Check the documentation for the placeholders your source actually uses.

  1. Why does loans["income"].astype(float) fail, and what does pd.to_numeric(loans["income"].str.replace(",", ""), errors="coerce") give instead?
Answer

"61,500" and "" cannot be parsed as floats, so astype raises an error. Removing the comma first and using errors="coerce" turns "61,500" into 61500.0 and the empty strings into NaN, which pandas then counts as missing.

Test yourself

Why does loans.isna().sum() report 0 missing incomes although two are empty?

  1. pandas has a bug
  2. Empty strings are values, not NaN
  3. The income column is numeric
  4. info() hides them

A column has the same value in every row. What does it tell a model?

  1. A lot, because it is consistent
  2. Nothing: a constant column carries no information
  3. It is the label
  4. It is an id

Why read random rows with sample() instead of only head()?

  1. sample() is faster
  2. Files are often sorted, so the first rows are not representative
  3. head() changes the data
  4. sample() removes duplicates

Two rows have different ids but identical values in every other column. What should you do first?

  1. Delete one immediately
  2. Keep both without thinking
  3. Find out from the data owner whether they are one event recorded twice or two real events
  4. Change one id