Python 3.4+statisticsfunction
statistics.median_grouped
median
Median of grouped continuous data, interpolated within the interval.
Syntax
statistics.median_grouped(data, interval=1)
It does not return the median of your numbers but an estimate of the median of an underlying continuous variable: every value is treated as the midpoint of a class of width interval, and the answer is interpolated inside that class. So use it only for data already rounded onto a grid (scores, age in whole years, instrument readings with a fixed step), and make interval match that step — a wrong interval yields a plausible-looking but meaningless number. For raw measurements plain median() is the right call.
Examples
import statistics
print(statistics.median_grouped([1, 2, 2, 3, 4, 4, 4])) # → 3.0
print(statistics.median_grouped([1, 1, 2, 2, 2, 2])) # → 1.75
print(statistics.median([1, 1, 2, 2, 2, 2])) # → 2.0
print(statistics.median_grouped([10, 20, 30, 40], interval=10)) # → 25.0
print(statistics.median_grouped([])) # → StatisticsError
See also
Open in the full glossarydocs.python.orgReport an inaccuracy