Descriptive summaries
Summarize measurements, timings, scores, or other numeric observations.
Python · Module
Python’s statistics module provides standard-library functions for averages, central tendency, spread, relationships, quantiles, and normal distributions over numeric data.
The module supplies functions for common statistics on numeric data, including averages, central values, spread, relationships, and probability distributions. Most functions accept an iterable, while specific functions distinguish population calculations from sample calculations.
import statistics
statistics.mean(data)| Name | Type | Description |
|---|---|---|
data | iterable of Real-valued numbers | An iterable or sequence of numeric values; individual functions may also accept a reference mean, population mean, weights, or related options. |
xbar | Real, optional | Optional precomputed sample mean for functions such as variance() and stdev(). |
mu | Real, optional | Optional population mean for functions such as pvariance() and pstdev(). |
The selected statistical function returns its documented numeric result or distribution-related object; exact return type depends on the function.
import statistics
values = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]
print(statistics.mean(values))
print(statistics.median(values))
print(statistics.variance(values))Computes an arithmetic mean, median, and sample variance for numeric data.
Summarize measurements, timings, scores, or other numeric observations.
Compare variation in a sample or an entire population.
Estimate distribution boundaries and relationships between paired observations.
Model or manipulate a normal distribution.
mean(data) returns the sample arithmetic mean: the sum of the values divided by their count. It accepts a sequence or iterable of real-valued numbers.
Follow-up: How would you calculate the population spread? Use pvariance() and pstdev(), rather than the sample-oriented variance() and stdev().
median(data) returns the middle value using the common method that averages the two central values when the data count is even. It operates on numeric data.
Follow-up: Which function preserves the lower or upper central value instead? Use median_low() or median_high().
variance(data, xbar=None) returns sample variance and requires an iterable containing at least two real-valued numbers. Passing xbar can provide a precomputed mean.
Follow-up: What corresponding function computes population variance? Use pvariance(data, mu=None), optionally supplying the population mean as mu.
meanfmeangeometric_meanharmonic_meanmedianmedian_lowmedian_highmedian_grouped