How to calculate median in python
To find the median of a statistical distribution in python, you can use the median() function of the statistics module.
median(x)
The input parameter (x) is the distribution of values.
The median() function returns the median value of the distribution as output.
Note. To use this function you need to import it into python from the statistics module using the import or from import statement.
Examples
Example 1
To find the median of the following distribution 1,3,5,6,7
>>> from statistics import median
>>> data=[1,3,5,6,7]
>>> median(data)
The distribution is assigned to the variable data
The output of the function median() is
5
The median of the distribution is number 5.
Example 2
This distribution is made up of an even number of elements 1,2,3,4,5,6
>>> from statistics import median
>>> data=[1,2,3,4,5,6]
>>> median(data)
The output of the function is
3.5
In this case the median value lies between the third and fourth element which have the value 3 and 4 respectively. Their average is 3.5.
How to find the low and high median. To select only the low median or the high median, you can use the median_low() or median_high () function, respectively, in place of the median() function. The median_low() function returns the low median of the distribution (3) while the median_high() function returns the high median (4).