Skip to content
Analyzing and Visualizing Data

Missing Measurements


Learning Objectives

  • You can distinguish a missing measurement from a recorded zero.
  • You can select observations with measured durations and report how many were omitted.
  • You can explain how missing records and the collection process limit a conclusion.

Our summaries have used a table with a duration in every row. A file from another source may contain a session whose duration was not recorded. This chapter establishes which rows can contribute to a calculation and how to report the omitted observations before presenting the results.

Missing Values and Recorded Zeros

Create missing-sessions.csv in data-analysis with these contents:

subject,minutes
Programming,20
Mathematics,40
Programming,
Languages,0

The third data row records a Programming session without a duration. The final row explicitly records zero minutes. In this example, that means no time was spent in the recorded session. The blank field tells us no duration; it does not establish that the duration was zero.

Replace analysis.py with this code to read and inspect the small file:

import pandas as pd
sessions = pd.read_csv("missing-sessions.csv")
print(sessions)

The table displays as:

subject minutes
0 Programming 20.0
1 Mathematics 40.0
2 Programming NaN
3 Languages 0.0

Here, pandas uses NaN to represent the missing numeric value. The measured durations display as floating-point values, such as 20.0. They still represent the same number of minutes.

For the calculations in this part, use this rule:

Include rows with a recorded duration. Omit rows missing a duration, and report how many were omitted.

The three contributing durations are 20, 40, and 0. Their mean is (20 + 40 + 0) / 3, or 20 minutes. Replacing the missing value with zero would instead divide the same total by four and report 15 minutes. That would add a measurement the file does not contain.

Choosing Contributing Records

0 / 5 points

A CSV contains these session records:

subject,minutes
Programming,18
Mathematics,36
Programming,
Languages,0

For duration calculations, omit rows missing minutes and retain recorded zeros. Which usable count and mean follow this rule?

Selecting Measured Durations

The dropna method can select the measured rows. Add these lines to analysis.py after reading the file:

usable = sessions.dropna(subset=["minutes"])
print(usable)

The output is:

subject minutes
0 Programming 20.0
1 Mathematics 40.0
3 Languages 0.0

The subset argument names the columns to check for missing values. Here it contains only "minutes". The returned table keeps the retained rows’ columns, original index labels, and order. The original sessions table is unchanged.

If another column has a missing value, this rule still checks only the duration column. Calling dropna() without subset would also check the other columns and could omit a row with a measured duration.

In small tables created directly in Python, None can represent a missing field. Pandas recognizes it as missing, just as it recognizes the blank numeric CSV cell. Apply the duration selection to the supplied table in the next exercise.

Keeping Sessions with Recorded Durations

0 / 20 points

Complete usable_durations(sessions). Return the rows whose minutes value is present. Keep recorded zeros and rows missing a value in another column.

For durations 20, missing, and 0, keep the first and third rows. Preserve all columns, index labels, and row order; leave sessions unchanged. If no duration is present, return an empty table with the original columns. Missing durations are pandas missing values, as when reading blank numeric CSV cells.


Inspecting missing fields

To inspect which rows lack a duration, print sessions["minutes"].isna(). It produces a Boolean value for each row: True for a missing field and False for a present value, including zero.

Reporting the Contributing Rows

Pandas can skip missing values when calculating sums, means, medians, and percentiles. A numerical result alone therefore does not tell us whether all durations were present. Selecting the usable rows and reporting their count makes the included observations visible.

Continue the local example with these calculations:

print("Usable sessions:", len(usable))
print("Omitted sessions:", len(sessions) - len(usable))
print("Recorded minutes:", usable["minutes"].sum())
print("Mean minutes:", usable["minutes"].mean())

The output is:

Usable sessions: 3
Omitted sessions: 1
Recorded minutes: 60.0
Mean minutes: 20.0

The next starter already calls the working selection function and supplies the report’s dictionary layout. Replace its None placeholders with the retained count, omitted count, and recorded total. Use usable for the retained count and total; subtract its row count from the original count to find how many were omitted. For an empty selection, its count and total are both zero.

A Report of Recorded Durations

0 / 25 points

Complete duration_report(sessions). The starter calls the supplied usable_durations helper to keep rows with a recorded minutes value and supplies the dictionary layout. Replace its three None placeholders with the requested counts and total. Return a dictionary with exactly these keys:

  • usable: the number of retained rows;
  • omitted: the number of rows whose minutes value is missing;
  • total_minutes: the total minutes in the retained rows.

For durations 20, missing, and 0, return {"usable": 2, "omitted": 1, "total_minutes": 20}. Omit only missing durations; keep zeros, repeated rows, and rows missing another field. Leave sessions unchanged. Durations are numeric or pandas missing values. An empty input produces three zeros.


A present value can still be wrong

dropna checks whether a value is missing. It would retain a negative duration, although a negative number cannot represent the time spent in these sessions. Check such a value against its source before using it. The exercises here use nonnegative recorded durations.

Conclusions from Incomplete Records

The usable records contain 60 recorded minutes and have a mean duration of 20 minutes. We cannot calculate the total or mean duration of all four sessions because one duration is unknown. Omitting the incomplete row lets us describe the measured sessions; it does not recover the missing measurement.

Some sessions may never reach the file. Suppose a logging tool loses records more often for long sessions. Those sessions would be less likely to appear in the table, so the mean of the remaining durations could understate the mean of all sessions. Removing rows with missing durations would not fix the collection problem.

Before describing a result as a summary of all study sessions, find out how the records were collected and which sessions could be absent. Report the usable and omitted counts alongside the calculation, while keeping the conclusion limited to the observations those counts describe.

Interpreting a Dataset

0 / 5 points

A student records only study sessions in the university library. The file contains eight sessions, every duration is present and nonnegative, and their mean is 45 minutes. No records were collected for the student’s sessions elsewhere.

Which conclusion is supported by this dataset?

We now have the pandas steps needed to prepare a result: inspect the records, select the contributing observations, and calculate the appropriate summary. In Bar Charts, seaborn will turn a prepared table into a labeled visual comparison. The same rule about usable and omitted observations applies to the values shown in a chart.

Check Your Understanding

  1. How would replacing missing durations with zeros change which rows contribute to the mean? Why can the total remain the same while the mean changes?
  2. Why can dropna() remove a usable duration that dropna(subset=["minutes"]) would retain?
  3. A log includes only sessions whose timer was stopped normally. What would you need to know before reporting its mean as the average for all sessions?