Skip to content

Lesson 6 ยท First Look at Data

Missing Values and Data Quality

Find hidden missing values, profile columns, check whether gaps differ by group, understand why data goes missing and screen for duplicates and bad values.

Most real datasets have gaps and errors. During exploration your job is not to repair everything at once. It is to measure the problems, find out whether they follow a pattern, and judge how much they affect your question.

Find disguised missing values

Missing data does not always look empty. Systems and people use stand-ins: "", "N/A", "unknown", "-", 0, -1, 999 or a default date such as 1900-01-01.

Python Count hidden missing values

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The markers are defined per column. Zero can be a real income in one system and a placeholder in another. Confirm each marker with someone who knows the data.

Profile every column

A profile is a compact summary of each column: how many values are present, how many are distinct and what the common ones are. In pandas you can build one in a few lines.

Python A small column profile in pandas

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the profile for warning signs. A column with one distinct value carries no information for comparisons. A column with as many distinct values as rows may be an identifier. app and App are one category written in two ways. A mostly empty discount may mean "no discount", which is not the same as "unknown".

Why is the value missing?

The reason matters more than the percentage. Statisticians describe three situations.

Three kinds of missingness

Completely at random Unrelated to any data
At random Explained by known columns
Not at random Depends on the unseen value

The data alone cannot always tell them apart

1 / 2

Examples: a sensor that loses data packets purely by chance is close to completely at random. If app users skip a form field more often than web users, the platform is recorded, and within each platform the skipping has nothing to do with the answer, the gap is missing at random. If people with high incomes avoid the income question, it is not at random, and the observed average is likely to be too low.

Check whether gaps differ by group

You cannot see the missing values, but you can see where they occur.

Python Missing share by group

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

App ages are missing four times as often. If you drop all rows without an age, you remove many app users, and the remaining mean leans toward web users. Also check missingness over time: a sudden jump can mark a broken form or a changed system.

What to do during exploration

  • Leave the gaps in place and report counts next to every summary.
  • Compare results with and without the affected rows.
  • Add a flag such as age_missing so you can study the gap itself.
  • Avoid filling values until you know why they are missing. Filling with the mean hides uncertainty and squeezes the distribution.

The pandas missing data lesson shows the mechanics of dropping and filling.

Other quality dimensions

Dimension Question Example check
Validity Is the value allowed? Age between 0 and 120
Uniqueness Is each thing recorded once? No repeated key
Consistency Do columns agree? Ship date not before order date
Accuracy Does it match reality? Compare a sample with source records
Timeliness Is it recent enough? Latest date in the table
SQL Rule checks in SQL

Press Run (or Ctrl + Enter) to see the result here.

Notice that shipped < ordered is never true when shipped is NULL. Unshipped orders pass that rule silently, so count the nulls separately, as the last line does.

Do not report only a clean subset without saying so. State how many rows you excluded, the rule you used and whether the conclusion changes when you include them.

Empty by design

Some gaps are correct. A "cancellation date" is empty for customers who never canceled. A "second address line" is empty because most addresses need one line. These are not quality problems, and filling them would create false information.

So ask a further question for each column: should this row have a value? If only some rows should, measure the missing share among those rows. For example, check shipped only among orders whose status says they were shipped. A gap there is a real contradiction.

Keep a quality log

For each problem, record the column, the rule, the number of affected rows, your decision and who confirmed it. A simple table is enough. The log protects you when someone asks why your total differs from theirs, and it helps the data owner fix the source so that the same problem does not return next month.

Try it yourself

In the first example, add a column for signup date that uses 1900-01-01 as a placeholder, and extend is_missing to detect it. Then count how many rows have at least one missing value. Compare that number with the share missing in each single column. Dropping every incomplete row can remove far more data than any one column suggests.

Gaps and your question

Judge each gap against the question you are answering. A mostly empty "middle name" column is harmless in a sales study. Even a small share of missing amounts can matter if the gaps fall on the largest orders, for example because large orders are entered by hand later. So look at what the incomplete rows have in common: dates, sources, stores or sizes. Then say how the missing values could move your result up or down. If you cannot tell, say that too.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which of these can be a hidden missing value?

    Many systems use special values instead of leaving a field empty.
  2. 2. People with high incomes tend to skip the income question. What kind of missingness is this?

    The chance of being missing depends on the unobserved value itself.
  3. 3. Why compare the missing share between groups?

    Dropping rows can change the mix of groups in your data.
  4. 4. What is a drawback of filling gaps with the mean?

    Many identical filled values pile up at the center.

Summary

  • Search for placeholders, not only empty cells, and define them per column.
  • Profile columns for gaps, distinct counts and inconsistent spellings.
  • The reason for missingness decides which treatments are reasonable.
  • Check gaps across groups and time before you drop or fill.
  • Test validity, uniqueness and consistency rules, and report what you excluded.

Next: Center and Spread of One Variable

Your progress is saved in this browser. No account needed.