Python Statistics Module
The statistics module provides functions for calculating mathematical statistics of numeric (Real-valued) data.
Importing the Module
import statistics
Averages and Measures of Central Location
mean()
Return the sample arithmetic mean of data.
import statistics
data = [1, 2, 3, 4, 4]
print(statistics.mean(data)) # Output: 2.8
median()
Return the median (middle value) of numeric data, using the common "mean of middle two" method.
import statistics
data = [1, 3, 5, 7]
print(statistics.median(data)) # Output: 4.0
mode()
Return the single most common data point from discrete or nominal data.
import statistics
data = [1, 2, 2, 3, 4, 2]
print(statistics.mode(data)) # Output: 2
Measures of Spread
stdev()
Return the square root of the sample variance.
import statistics
data = [1.5, 2.5, 2.5, 2.75, 3.25, 4.75]
print(statistics.stdev(data))
variance()
Return the sample variance of data, an iterable of at least two real-valued numbers.
import statistics
data = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]
print(statistics.variance(data))