statistics — statistical functions

Python statistics mean, median, and standard deviation, including the sample versus population distinction and empty or single-value errors.

モジュールページの本文は現在英語です。

The statistics module is part of the Python standard library. It is not a third-party package such as NumPy or pandas, and it is not the math module. Import it with `import statistics`. The functions work on ordinary Python numeric data.

Sample and population measures are different functions. stdev and variance use a sample denominator (n - 1). pstdev and pvariance use a population denominator (n). This page covers mean, median, and those two standard deviations, including what happens with empty or single-value input.

Examples were checked against Python 3.13. See the official documentation for the complete API.

Python reference overview →

mean and median

mean(data) is the arithmetic mean and requires at least one data point. median(data) returns the middle value when the length is odd, and the average of the two middle values when the length is even.

statistics.mean, statistics.median

statistics.mean(data)
statistics.median(data)

Compute the arithmetic mean or the median of a sequence of numeric data.

ParameterDescription
dataAn iterable of numbers. It is fully consumed. An empty input is an error for both of these functions.

Returns: A number. For a list of ints, mean and an even-length median often return a float, as in median([1, 2, 3, 4]) == 2.5.

Raises

  • statistics.StatisticsError — mean([]) raises StatisticsError: mean requires at least one data point.
  • These helpers do not ignore non-finite values for you. If the data may contain NaN, decide how to handle it before calling them.
python
import statistics

print(statistics.mean([1, 2, 3, 4]))
print(statistics.median([1, 2, 3, 4]), statistics.median([1, 2, 3]))
try:
    statistics.mean([])
except statistics.StatisticsError as exc:
    print(f"{type(exc).__name__}: {exc}")

Expected output

2.5
2.5 2
StatisticsError: mean requires at least one data point

Official documentation →

Sample and population standard deviation

stdev(data) is the sample standard deviation and requires at least two data points. pstdev(data) is the population standard deviation. Use stdev when the data are a sample from a larger population, and pstdev when the data are the whole population you care about.

statistics.stdev, statistics.pstdev

statistics.stdev(data, xbar=None)
statistics.pstdev(data, mu=None)

Compute a standard deviation using a sample denominator (n - 1) or a population denominator (n). A single value is not enough for stdev.

ParameterDescription
dataAn iterable of numbers with at least two points for stdev, and at least one for pstdev.
xbar / muOptional precomputed mean. If omitted, the function computes the mean from data.

Returns: A float for ordinary Python ints and floats.

Raises

  • statistics.StatisticsError — stdev([10]) raises StatisticsError: stdev requires at least two data points.
  • For [1, 2, 3], stdev is 1.0 and pstdev is 0.816496580927726. The two results are not interchangeable.
  • mode and multimode are separate functions. They are not covered in depth here; an empty sequence has no mode.
python
import statistics

print(statistics.stdev([1, 2, 3]))
print(statistics.pstdev([1, 2, 3]))
try:
    statistics.stdev([10])
except statistics.StatisticsError as exc:
    print(f"{type(exc).__name__}: {exc}")

Expected output

1.0
0.816496580927726
StatisticsError: stdev requires at least two data points

Official documentation →