Python Tutorial (45) - statistics module

Time: Column:Python views:185

The Python statistics module is part of the standard library, offering numerous functions for basic statistical calculations.

Introduced in Python version 3.4, this module helps analyze and compute the statistical characteristics of datasets.

Importing the statistics Module

To use functions from the statistics module, you must first import it:

import statistics

Exploring the statistics Module

To 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']

statistics Module Methods

The statistics module provides the following methods for various calculations:

MethodDescription
statistics.harmonic_mean()Calculates the harmonic mean of a given dataset.
statistics.mean()Computes the arithmetic mean of the dataset.
statistics.median()Calculates the median (middle value) of the dataset.
statistics.median_grouped()Calculates the grouped median for a given grouped dataset.
statistics.median_high()Returns the high median of the dataset.
statistics.median_low()Returns the low median of the dataset.
statistics.mode()Computes the mode (the most frequent value) of the dataset.
statistics.pstdev()Calculates the population standard deviation of the dataset.
statistics.stdev()Computes the sample standard deviation of the dataset.
statistics.pvariance()Calculates the population variance of the dataset.
statistics.variance()Computes the sample variance of the dataset.
statistics.quantiles()Calculates the quantiles of the dataset. You can specify the number of quantiles (default is quartiles).

By using the statistics module, Python provides a powerful way to perform essential statistical operations without the need for third-party libraries.