Mean, Median, and Percentiles
Learning Objectives
- You can calculate and interpret a median duration.
- You can explain how a changed measurement can affect the mean and median differently.
- You can calculate quartiles using percentile fractions and interpret their units.
- You can use the interquartile range to describe the spread of the middle observations.
We have used means to compare average session lengths. Before presenting those results, we need to consider how the individual measurements affect the average. This chapter compares the mean with the median, then uses percentiles to describe positions and spread within the recorded durations.
Comparing the Mean and Median
The Writing durations in sessions.csv are 15, 25, and 110 minutes. Their mean is 50 minutes. The long session contributes to that mean, even though the other two sessions are shorter than 50 minutes.
The median is the middle value when the measurements are ordered from smallest to largest. For these three durations, it is 25 minutes. With an even number of measurements, the median is the mean of the two middle values. For example, the median of 10, 20, 30, and 100 is (20 + 30) / 2, or 25.
Use the original sessions.csv to compare the two summaries for Writing in analysis.py:
import pandas as pd
sessions = pd.read_csv("sessions.csv")mask = sessions["subject"] == "Writing"writing = sessions[mask]print(writing["minutes"].mean())print(writing["minutes"].median())The output is:
50.025.0median() calculates the middle value without requiring you to sort the table first.
Increasing the 110-minute duration would increase the mean while leaving the middle value at 25. Both summaries use the same recorded sessions. The median describes their middle duration; the mean describes their arithmetic average. Choosing a median does not remove the long session, and its length alone does not establish a recording error.
A median and a mean can react differently when one measurement changes. Trace that difference on a small dataset:
Changing One Measurement
0 / 5 points
A table contains three recorded journey durations: 12, 18, and 90 minutes. The 90-minute value is corrected to 120 minutes. No row is added or removed.
Which statement describes the mean and median before and after this correction?
Percentiles and Quartiles
A percentile describes a cutoff at a chosen position in ordered measurements. The 50th percentile is the median. The 25th and 75th percentiles are the lower and upper quartiles, often named Q1 and Q3. Together, they describe the interval around the middle half of the ordered measurements.
A Series has a quantile method for calculating these cutoffs. Its argument is a fraction from 0 to 1: the 75th percentile uses 0.75, rather than 75. Add this code to calculate quartiles for all 24 sessions in sessions:
minutes = sessions["minutes"]q1 = minutes.quantile(0.25)median = minutes.quantile(0.50)q3 = minutes.quantile(0.75)iqr = q3 - q1print("Q1:", q1)print("Median:", median)print("Q3:", q3)print("IQR:", iqr)The output is:
Q1: 25.0Median: 35.0Q3: 46.25IQR: 21.25These values are durations in minutes. Q3 is not 75% of the total study time. The interquartile range, or IQR, is Q3 - Q1: here, 21.25 minutes. It describes the spread of the middle observations, without showing the full range from the shortest session to the longest.
The value 46.25 does not appear in the CSV. Pandas uses linear interpolation by default: when a requested position falls between two measurements, it calculates a value between them. This is also how the median can lie between two middle values. With small samples and repeated values, a percentile need not put exactly the named percentage of observations below its cutoff.
The next exercise applies these calculations to reading durations. Its starter selects the duration column and supplies the dictionary layout. Replace the None placeholders with the quartile and IQR calculations, using all readings together rather than grouping them by genre.
Percentiles of Reading Durations
0 / 20 points
Complete duration_percentiles(readings). The starter selects the minutes column and supplies the dictionary layout. Replace its four None placeholders with calculations for q1, median, q3, and the IQR. Return a dictionary with exactly these keys:
q1: the 25th percentile of theminutescolumn;median: the 50th percentile;q3: the 75th percentile;iqr: the differenceq3 - q1.
Use the Series quantile method with fractions from 0 to 1 and its default linear interpolation. All results are numeric durations in minutes. For durations 10, 20, 30, and 100, return {"q1": 17.5, "median": 25.0, "q3": 47.5, "iqr": 30.0}.
The input has at least one row and contains recorded numeric durations with no missing values. Use all rows together, including repeated durations and recorded zeros; other columns do not take part in the calculation. Leave readings unchanged, including its row labels and order. Do not round the results.
Means, medians, and percentiles describe different aspects of the same measurements. Each requires us to know which measurements contributed. In Missing Measurements, we will handle records with unknown durations before using their summaries in charts.
Check Your Understanding
- How can a mean exceed most recorded durations without any recording error? What would the median describe in that dataset?
- A duration column has Q1 of 20 minutes and Q3 of 50 minutes. What is its IQR, and what part of the measurements does that describe?
- Why can
minutes.quantile(0.75)return a duration that no session recorded? Why does that result not imply that exactly 75% of the rows have smaller durations?