Skip to content

Python statistics Module

In data analysis and scientific computing, statistics is a very important tool.

Python provides a built-in statistics module specifically designed for handling basic statistical calculations. This article will detail the functions and usage of the statistics module, helping beginners quickly master how to use this module for basic statistical analysis.

The statistics module provides many commonly used statistical functions, such as mean, median, variance, standard deviation, etc.

To use statistics functions, you must first import:

import statistics

View the contents of the statistics module:

>>> import statistics
>>> dir(statistics)
['Counter', 'Decimal', 'Fraction', 'NormalDist', 'StatisticsError', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_coerce', '_convert', '_exact_ratio', '_fail_neg', '_find_lteq', '_find_rteq', '_isfinite', '_normal_dist_inv_cdf', '_ss', '_sum', 'bisect_left', 'bisect_right', 'erf', 'exp', 'fabs', 'fmean', 'fsum', 'geometric_mean', 'groupby', 'harmonic_mean', 'hypot', 'itemgetter', 'log', 'math', 'mean', 'median', 'median_grouped', 'median_high', 'median_low', 'mode', 'multimode', 'numbers', 'pstdev', 'pvariance', 'quantiles', 'random', 'sqrt', 'stdev', 'tau', 'variance']

The mean is the average value of all numbers in a dataset. The statistics module provides the mean() function to calculate the mean.

data = [1, 2, 3, 4, 5]
mean_value = statistics.mean(data)
print("Mean:", mean_value)

Output:

Mean: 3

The median is the value located at the middle position when the dataset is arranged in order of size. The statistics module provides the median() function to calculate the median.

data = [1, 2, 3, 4, 5]
median_value = statistics.median(data)
print("Median:", median_value)

Output:

Median: 3

If the dataset length is even, the median() function automatically calculates the average of the two middle numbers.

data = [1, 2, 3, 4]
median_value = statistics.median(data)
print("Median:", median_value)

Output:

Median: 2.5

The mode is the value that appears most frequently in the dataset. The statistics module provides the mode() function to calculate the mode.

data = [1, 2, 2, 3, 4]
mode_value = statistics.mode(data)
print("Mode:", mode_value)

Output:

Mode: 2

If there are no duplicate values in the dataset, the mode() function will raise a StatisticsError exception.

Variance is a measure of the dispersion of values in a dataset. The statistics module provides the variance() function to calculate variance.

data = [1, 2, 3, 4, 5]
variance_value = statistics.variance(data)
print("Variance:", variance_value)

Output:

Variance: 2.5

Standard deviation is the square root of the variance, used to measure the dispersion of a dataset. The statistics module provides the stdev() function to calculate standard deviation.

data = [1, 2, 3, 4, 5]
stdev_value = statistics.stdev(data)
print("Standard Deviation:", stdev_value)

Output:

Standard Deviation: 1.5811388300841898

The harmonic mean is a special type of average, suitable for calculating rates and similar scenarios. The statistics module provides the harmonic_mean() function to calculate the harmonic mean.

data = [1, 2, 4]
harmonic_mean_value = statistics.harmonic_mean(data)
print("Harmonic Mean:", harmonic_mean_value)

Output:

Harmonic Mean: 1.7142857142857142

The geometric mean is a type of average used for calculating growth rates or ratios. The statistics module provides the geometric_mean() function to calculate the geometric mean.

data = [1, 2, 4]
geometric_mean_value = statistics.geometric_mean(data)
print("Geometric Mean:", geometric_mean_value)

Output:

Geometric Mean: 2.0

The statistics module also provides the median_low() and median_high() functions, used to calculate the median low and median high of a dataset respectively.

data = [1, 2, 3, 4]
median_low_value = statistics.median_low(data)
median_high_value = statistics.median_high(data)
print("Median Low:", median_low_value)
print("Median High:", median_high_value)

Output:

Median Low: 2
Median High: 3

Quantiles are values that divide a dataset into equal parts. The statistics module provides the quantiles() function to calculate quantiles.

data = [1, 2, 3, 4, 5]
quantiles_value = statistics.quantiles(data, n=4)
print("Quartiles:", quantiles_value)

Output:

Quartiles: [1.5, 3.0, 4.5]

Method Description
statistics.harmonic_mean() Calculates the harmonic mean of the given dataset.
statistics.mean() Calculates the arithmetic mean of the dataset.
statistics.median() Calculates the median of the dataset.
statistics.median_grouped() Calculates the grouped median of the grouped dataset.
statistics.median_high() Calculates the high median of the given dataset.
statistics.median_low() Calculates the low median of the given dataset.
statistics.mode() Calculates the mode (most frequent value) of the dataset.
statistics.pstdev() Calculates the population standard deviation.
statistics.stdev() Calculates the standard deviation of the dataset.
statistics.pvariance() Calculates the population variance of the given dataset.
statistics.variance() Calculates the variance of the dataset.
statistics.quantiles() Calculates quantiles of the dataset, with configurable number of quantiles (default is quartiles).