TExcelTAD
Home › Statistics › Quartiles and Percentiles

Quartiles and Percentiles

Percentiles split sorted data into hundredths. The k-th percentile is a value such that about k% of the observations lie at or below it. If your test score is at the 90th percentile, you scored as well as or better than 90% of the test takers.

Quartiles are three special percentiles that split the data into four equal parts:

Quartile Percentile Meaning
Q1 (first, lower) 25th a quarter of the data is below it
Q2 (second) 50th the median
Q3 (third, upper) 75th three quarters of the data is below it

Together with the minimum and maximum they form the five-number summary behind a box plot.

Quartiles by hand (the "median of halves" method)

The textbook method for small datasets:

  1. Sort the data and find the median.
  2. Q1 is the median of the lower half, Q3 the median of the upper half (if n is odd, leave the overall median out of both halves).
python · try it yourself
import statistics

times = [31, 24, 28, 35, 22, 40, 27, 30, 26, 33, 29]   # 11 values
s = sorted(times)
n = len(s)
lower, upper = s[: n // 2], s[(n + 1) // 2 :]

print("sorted:", s)
print("lower half:", lower, "-> Q1 =", statistics.median(lower))
print("median Q2 =", statistics.median(s))
print("upper half:", upper, "-> Q3 =", statistics.median(upper))
sorted: [22, 24, 26, 27, 28, 29, 30, 31, 33, 35, 40]
lower half: [22, 24, 26, 27, 28] -> Q1 = 26
median Q2 = 29
upper half: [30, 31, 33, 35, 40] -> Q3 = 33

Percentiles in NumPy and pandas

For general percentiles we need interpolation: the k-th percentile often falls between two data values. NumPy's default ("linear") method places the k-th percentile at position (n − 1) × k / 100 in the sorted data (counting from 0) and interpolates between neighbours:

python · try it yourself
import numpy as np

times = np.array([31, 24, 28, 35, 22, 40, 27, 30, 26, 33, 29])
print("Q1, Q2, Q3:", np.percentile(times, [25, 50, 75]))
print("90th percentile:", np.percentile(times, 90))

# reproduce the 90th percentile by hand
s = np.sort(times)
pos = (len(s) - 1) * 0.90          # 9.0 -> exactly the element at index 9
print("position:", pos, "-> value:", s[int(pos)])
Q1, Q2, Q3: [26.5 29.  32. ]
90th percentile: 35.0
position: 9.0 -> value: 35

pandas uses the same default with fractions instead of percentages:

python · try it yourself
import pandas as pd

times = pd.Series([31, 24, 28, 35, 22, 40, 27, 30, 26, 33, 29])
print(times.quantile([0.25, 0.5, 0.75]))
0.25    26.5
0.50    29.0
0.75    32.0
dtype: float64

Different methods, slightly different answers

There is no single universal definition of a quartile. Statistical software offers many methods, and for small samples they disagree a little. Python's statistics.quantiles uses the "exclusive" method by default, which differs from NumPy's default:

python · try it yourself
import statistics
import numpy as np

data = [3, 7, 8, 5, 12, 14, 21, 13, 18]

print("statistics, exclusive:", statistics.quantiles(data, n=4))
print("statistics, inclusive:", statistics.quantiles(data, n=4, method="inclusive"))
print("numpy linear (default):", np.percentile(data, [25, 50, 75]).tolist())
print("numpy 'weibull'       :", np.percentile(data, [25, 50, 75], method="weibull").tolist())
statistics, exclusive: [6.0, 12.0, 16.0]
statistics, inclusive: [7.0, 12.0, 14.0]
numpy linear (default): [7.0, 12.0, 14.0]
numpy 'weibull'       : [6.0, 12.0, 16.0]

The "inclusive" method of statistics equals NumPy's default, and the "exclusive" method equals NumPy's "weibull" method. For large datasets the differences become negligible. Just be consistent within an analysis and say which tool you used.

Percentile rank: where does a value stand?

The reverse question, "what percentage of values are at or below x?", is the percentile rank. SciPy computes it with percentileofscore:

python · try it yourself
import numpy as np
from scipy import stats

rng = np.random.default_rng(17)
scores = rng.normal(65, 12, size=400).round()       # exam scores of 400 students

my_score = 82
rank = stats.percentileofscore(scores, my_score, kind="weak")
print(f"a score of {my_score} is at the {rank:.1f}th percentile")
print(f"the 90th percentile score is {np.percentile(scores, 90):.0f}")
a score of 82 is at the 92.5th percentile
the 90th percentile score is 80

kind="weak" counts values at or below the score, matching the definition above.

Deciles and other quantiles

Percentiles, quartiles and deciles (tenths) are all quantiles: cut points that split the data into equal-sized groups. statistics.quantiles(data, n=10) returns the nine decile cut points, for example. Quantiles are widely used in growth charts for children, income statistics ("the top 10%") and service-level targets ("95% of requests answered within 200 ms").

Test yourself

Your running time is at the 30th percentile of all finishers (times are sorted from fastest to slowest). What does that mean?

  1. You were slower than 70% of runners
  2. 30% of finishers had a time equal to or less than yours
  3. You finished 30th
  4. You ran 30% faster than average

Which quartile is the same as the median?

  1. Q1
  2. Q2
  3. Q3
  4. Q4

Why can statistics.quantiles and np.percentile return different quartiles for the same small dataset?

  1. One of them has a bug
  2. They use different interpolation methods by default
  3. NumPy sorts the data differently
  4. statistics ignores the largest value