Data Science

How to calculate mean in python

Written by Admin · 1 min read >

In this article, you can find how to calculate mean in python and also other alternatives like trimmed mean, weighted mean and median. The alternatives are useful when data have extreme values that may have a big influence on the mean. For example the mean of the following numbers: 1,2,3,4,5,100 is 19.16, but if we remove 100 which is an extreme value the mean would be 3.0 which is a big difference.

Table 1.0

The following table contains bitcoin historical prices and will be used to exemplify the concepts from this article.


The symbol for the mean is:

The mean is the sum of all values divided by the number of values.

And the formula can be found below:

The python code to calculate the mean price for Table 1.0 is : 

Trimmed mean

Trimmed mean is the mean calculated after discarding a fixed number of ordered values at each end. This number of points to be discarded is usually given as a percentage of the total number of points, but may also be given as a fixed number of points.

Trimmed mean it is used to eliminate the influence of extreme values.

The formula can be found below:

Python code to calculate the Trimmed mean:

Weighted mean

The weighted mean is a type of mean that is calculated by multiplying the weight (or probability) associated with a particular event or outcome with its associated quantitative outcome and then summing all the products together. It is very useful when calculating a theoretically expected outcome where each outcome has a different probability of occurring.

Weighted mean it is calculated as follow:

Python code:


Median is the middle number on a sorted list of the data.

The basic feature of the median in describing data compared to the mean is that it is not skewed by a small proportion of extremely large or small values.

Python code is: