AppDividend
Latest Code Tutorials

# Python Median Function Example | Python Statistics Module

Python median() is an inbuilt function of the statistics module that can be used to calculate the median value from an unsorted data-list. The most significant advantage of using the median() method is that the data-list does not need to be sorted before being sent as a parameter to the median() function.

## #How to calculate median in Python

If you are looking for a function that calculates the median() in Python then the median() function is your answer.

The following is a statistical formula to calculate the median of any dataset.

```Median = {(n + 1) / 2}th Value
```

The statistics median is the quick measure to find the central location of the data sequence, list or any iterator.

Note:

When the number of data points in the given sequence or list or iterator is odd, an exact middle data point (number) is returned.

## Python Median Function Example

How do you find the median of a list in Python? Well, the list can be of any size, and the numbers are not guaranteed to be in a particular order.

If the list contains an even number of items, the function should return an average of the middle two.

Python 3.4 has `statistics.median`function.

The median() function returns the median (middle value) of numeric data.

When a number of data points are odd, return the middle data point. When the number of data points is even, a median is interpolated by taking the average of the two middle values.

Median is a value that separates a higher half of the data or probability distribution from the lower half. If the list contains an even number of elements, the function should return the average of the middle two.

When the number of items in the list or tuple or any iterator is odd, it returns the middle data point.

If the number of data points in the list or tuple is even, the median is interpolated by taking an average of the two middle values.

## #Finding the median of a list in Python

Okay, let’s define a list with the odd number of items.

```# app.py

import statistics

listA = [19, 46, 21, 18, 30]
print(statistics.median(listA))```

In the above-written code, you can see that 21 is the median number and you can run the above file and check the output in the console.

Now, let’s find a median where the list contains an even number of items.

```# app.py

listA = [6, 46, 21, 19, 18, 30]
print(statistics.median(listA))```

See the output.

We can find the median of any dataset, that can be list or tuple or an iterable with a set of numeric values. If the items are empty or null, then StatisticsError is raised.

## #Finding the median of a tuple in Python

Let’s define a tuple and then find its median.

```# app.py

import statistics

tupleA = (21, 19, 19, 21, 18)
print(statistics.median(tupleA))```

See the output.

## #More Examples of Python Statistical Mean Function

Write the following code inside the app.py file.

```# app.py

import statistics
from fractions import Fraction as fr

data1 = (1, 2, 2, 3, 3, 3, 4, 4, 4, 4)
print(statistics.median(data1))

data2 = (2.1, 1.9, 2.1, 1.8, 2.9)
print(statistics.median(data2))

data3 = (fr(19, 21), fr(18, 21), fr(19, 21), fr(21, 46))
print(statistics.median(data3))

data4 = (-21, -22, -21, -29, -18, -19)
print(statistics.median(data4))```

See the output.

## #StatisticsError in median() function

Okay, we get the StatisticsError if the list is empty. See the following code.

```# app.py

import statistics

data = []

print(statistics.median(data))

```

See the following output.

```➜  pyt python3 app.py
Traceback (most recent call last):
File "app.py", line 5, in
print(statistics.median(data))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/statistics.py", line 380, in median
raise StatisticsError("no median for empty data")
statistics.StatisticsError: no median for empty data
➜  pyt
```

From the StatisticsError, you can say that no median for empty data.

## #Using the numpy module

To compute the median, we can also use the numpy module.

```# app.py

import statistics
import numpy as np

dataset = [21, 19, 11, 21, 19, 46, 29]
op = np.median(dataset)

print(op)
```

See the following output.

```➜  pyt python3 app.py
21.0
➜  pyt
```

Any value in the dataset that is at an abnormal distance from all the other values can be termed as the outlier.

Outliers generally tend to skew a mean radically.

Outliers can be present in a dataset with very high value or with the deficient value.

So, the Median is the value that lies at the center.

Median is described as the middle number when all numbers are sorted from smallest to largest. In simple translation, sort all numbers in a list from the smallest one to the largest one. Whichever number is in the middle is the median.

In case there even a number of items in a data set, a median is an average of the two values that lie in the center.

Finally, Python Median Function Example | Python Statistics Tutorial is over.

This site uses Akismet to reduce spam. Learn how your comment data is processed.