सामग्री पर जाएँ

लेसन 6 · Missing Values

Finding Missing Values

Find missing values, including hidden ones like N/A, -999, blank spaces and default dates, and see how Python, pandas and averages treat missing data.

Some missing values are obvious: an empty cell. Many are disguised as ordinary data. A 0, a -999, the word unknown or the date 1900-01-01 may all mean "we do not know". If you do not find them, they take part in your sums and averages as if they were real.

Ways data says "nothing"

Form Example
True empty None in Python, NULL in SQL, NaN for missing numbers in pandas
Text markers N/A, null, -, ?, unknown
Blank-looking "", spaces, a tab
Number placeholders -1, -999, 9999
Default values 1900-01-01, 1970-01-01, the first option in a drop-down
Absent rows A day with no record at all

Which values are placeholders depends on the dataset. A 0 can be a real zero (no purchases) or a stand-in for "not measured". Check the documentation or ask.

Normalize missing markers

Choose your list of markers per column, then convert them to one real missing value. Keep a count of what you converted.

Python Convert hidden markers to None

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 visits value 0 stays: it is a real zero. Only the markers we listed became None.

How missing values change results

Python Zero, placeholder or missing?

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 first average is nonsense and easy to spot. The second looks believable, which makes it more dangerous. When you exclude missing values, report how many records the result uses.

NaN and None in pandas

pandas has several missing markers, including NaN for numbers and NaT for dates. Use isna() to detect them. NaN is not equal to anything, even itself, so value == float("nan") is always false.

Python pandas: detect 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 first count finds only the true empties. After normalizing, the hidden ones appear. isna().mean() gives the share missing per column. pandas also skips missing values in many calculations by default, so check count() alongside any average. See missing data in pandas for more.

Missing rows

A table can have no empty cells and still be missing data. If a sensor sends one reading per hour, an outage may leave no row at all. Compare against what should exist:

Python Find dates with no row

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.

Do the same with lists of stores, products or survey participants. A missing sales row may mean zero sales or a failed upload. These need different fixes.

Where is it missing?

Break missingness down by group: source, date, device, region or form version. If one branch has no phone numbers after a certain week, you have found a process change. This clue matters in the next lesson.

Common mistake: replacing placeholders across the whole table at once. Replacing every 999 would also erase a real price or quantity of 999. Apply markers column by column.

A search routine

Use this routine on every new column:

  1. Count true empties.
  2. Count blank strings after trimming.
  3. List the frequent text values and look for words that mean "nothing".
  4. For numbers, look at the smallest and largest values and at any value that repeats suspiciously often.
  5. For dates, look for the same date on many unrelated records.
  6. Read the documentation or ask the owner what each candidate means.

Only after step six should you convert anything. A value such as 0000 could be a placeholder in one system and a real code in another.

Keep the reason when you can

Sometimes the markers carry useful information. refused, not asked and sensor error are all missing values, but they mean different things. If you turn all of them into one empty value, you lose that difference. A simple solution is to keep two columns: the cleaned value, which is empty, and a reason column that says why.

raw_income income income_missing_reason
52000 52000
refused (missing) refused
n/a (missing) not applicable

This invented table shows the pattern.

Missing values in other tools

SQL uses NULL, which does not equal anything, so you test it with IS NULL. In spreadsheets, an empty cell and a formula that returns empty text can behave differently in counting functions. In JSON, a key can be absent or present with the value null, and your program should treat the two cases on purpose. When data moves between tools, check that empties are still empties and have not turned into the text NULL, nan or None.

That last problem is common after careless exports, so include those words in your list of suspects.

Check after cleaning

When you finish converting markers, profile the column again. The missing count should equal the true empties plus the markers you converted. If it is higher, a rule caught more than you intended, so look at the original text of the extra rows. If you still see words such as none among the frequent values, your list is incomplete.

Remember too that a type conversion in a later step can create new missing values. Count again after that step, and keep the two kinds of gaps apart in your report: values that arrived empty, and values that you could not read.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. Why can a zero used as a placeholder be worse than -999?

    An absurd value gets noticed. A believable one quietly shifts results.
  2. 2. How should you detect NaN in pandas?

    NaN is not equal to itself, so equality tests fail. isna() works for pandas missing markers.
  3. 3. A daily table has no row for one date. What should you do?

    An absent row can mean zero activity or lost data, and only investigation can tell.

Summary

  • Look for markers, blanks, placeholders and defaults.
  • Define missing markers per column and count conversions.
  • Report how many records each result uses.
  • Check for absent rows and group patterns.

Next: Why Data Is Missing

आपकी प्रगति इसी ब्राउज़र में सेव होती है। खाते की ज़रूरत नहीं है।