Python Statistics Module

In python, the statistics module has different functions to perform the mathematical statistics of numeric data.

 

Following is the example of importing a built-in statistics module to calculate the mathematical statistics of numeric data.

 

import statistics

print(statistics.mean([10, 15, 20, 25, 30]))
print(statistics.mean([4, 5, 6, 7, 8, 9, 10, 11]))

If you observe the above example, we imported statistics module using import keyword and used mean() method to calculate the arithmetic mean (average) of the numbers in the list.

 

The above statistics module example will return the result as shown below.

 

20
7.5

Like the mean() method, the statistics module has different methods to calculate mathematical statistics of numeric data.

Median() Method

The statistics module median() method is useful to calculate the median (middle) value of the numeric data in the list.

 

Following is the example of using the statistics module median() method in python.

 

import statistics

print(statistics.median([10, 15, 20, 25, 30]))
print(statistics.median([4, 5, 6, 7, 8, 9, 10, 11]))

The above statistics module median() method example will return the middle value of numeric data in the list as shown below.

 

20
7.5

Mode() Method

The statistics module mode() method is useful to return the most common data point on the list.

 

Following is an example of using the mode() function to get the common data point from the python list.

 

import statistics

print(statistics.mode([10, 15, 10, 25, 30]))
print(statistics.mode([4, 5, 5, 7, 7, 9, 10, 11]))
print(statistics.mode(['tut', 'lane', 'tut', 'tutlane']))

The above statistics module mode() function example will return the most common data points from the list as shown below.

 

10
5
tut

Stdev() Method

The statistics module stdev() method is useful to calculate the standard deviation of the given data.

 

Following is an example of the statistics module stdev() method to calculate python's sample data's standard deviation.

 

import statistics

print(statistics.stdev([10, 15, 10, 25, 30]))
print(statistics.stdev([4, 5, 5, 7, 7, 9, 10, 11]))

The above statistics module example will return the result as shown below.

 

9.082951062292475
2.5495097567963922

Variance() Method

The statistics module variance() method is useful to calculate the variance from a given sample data.

 

Following is an example of the statistics module variance() method to calculate the variance from python's sample data.

 

import statistics

print(statistics.variance([10, 15, 10, 25, 30]))
print(statistics.variance([4, 5, 5, 7, 7, 9, 10, 11]))

The above statistics module example will return the result as shown below.

 

82.5
6.5

The following table lists the different statistics module methods to calculate the mathematical statistics of numeric data based on our requirements.

 

MethodDescription
mean() It will calculate the arithmetic mean (average) of the numbers in the list.
harmonic_mean() It will calculate the harmonic mean (central location) of the numbers in the list.
median() It will calculate the median (middle) value of the numeric data in the list.
median_high() It will calculate the high median value of the numeric data in the list.
median_low() It will calculate the low median value of the numeric data in the list.
median_grouped() It will calculate the median of grouped continuous numeric data in the list.
mode() It will return the most common data point in the list.
stdev() It will calculate the standard deviation of the given data.
pstdev() It will calculate the standard deviation from an entire population.
variance() It will calculate the variance from a given sample data.
pvariance() It will calculate the variance of an entire population.