# median() function in Python statistics module

## median() function in Python statistics module

Python is a very popular language when it comes to data analysis and statistics. Luckily, Python3 provide statistics module, which comes with very useful functions like mean(), median(), mode() etc.
median() function in the statistics module can be used to calculate median value from an unsorted data-list. The biggest advantage of using median() function is that the data-list does not need to be sorted before being sent as parameter to the median() function.
Median is the value that separates the higher half of a data sample or probability distribution from the lower half. For a dataset, it may be thought of as the middle value. The median is the measure of the central tendency of the properties of a data-set in statistics and probability theory. Median has a very big advantage over Mean, which is the median value is not skewed so much by extremely large or small values. The median value is either contained in the data-set of values provided or it doesn’t sway too much from the data provided.
For odd set of elements, the median value is the middle one.
For even set of elements, the median value is the mean of two middle elements.

Median can be represented by the following formula :



Syntax : median( [data-set] )
Parameters :
[data-set] : List or tuple or an iterable with a set of numeric values
Returns : Return the median (middle value) of the iterable containing the data
Exceptions : StatisticsError is raised when iterable passed is empty or when list is null.

Code #1 : Working

 # Python code to demonstrate the  # working of median() function. # importing statistics module import statistics # unsorted list of random integers data1 = [2, -2, 3, 6, 9, 4, 5, -1] # Printing median of the # random data-set print("Median of data-set is : % s "         % (statistics.median(data1)))

Output :

Median of data-set is : 3.5

Code #2 :

 # Python code to demonstrate the # working of median() on various # range of data-sets # importing the statistics module from statistics import median # Importing fractions module as fr from fractions import Fraction as fr # tuple of positive integer numbers data1 = (2, 3, 4, 5, 7, 9, 11) # tuple of floating point values data2 = (2.4, 5.1, 6.7, 8.9) # tuple of fractional numbers data3 = (fr(1, 2), fr(44, 12),          fr(10, 3), fr(2, 3)) # tuple of a set of  negative integers data4 = (-5, -1, -12, -19, -3) # tuple of set of positive # and negative integers data5 = (-1, -2, -3, -4, 4, 3, 2, 1) # Printing the median of above datasets print("Median of data-set 1 is % s" % (median(data1))) print("Median of data-set 2 is % s" % (median(data2))) print("Median of data-set 3 is % s" % (median(data3))) print("Median of data-set 4 is % s" % (median(data4))) print("Median of data-set 5 is % s" % (median(data5)))

Output :

Median of data-set 1 is 5
Median of data-set 2 is 5.9
Median of data-set 3 is 2
Median of data-set 4 is -5
Median of data-set 5 is 0.0

Code #3 : Demonstrating StatisticsError

 # Python code to demonstrate # StatisticsError of median() # importing the statistics module from statistics import median # creating an empty data-set empty = [] # will raise StatisticsError print(median(empty))

Output :

Traceback (most recent call last):
File "/home/3c98774036f97845ee9f65f6d3571e49.py", line 12, in
print(median(empty))
File "/usr/lib/python3.5/statistics.py", line 353, in median
raise StatisticsError("no median for empty data")
statistics.StatisticsError: no median for empty data

Applications :
For practical applications, different measures of dispersion and population tendency are compared on the basis of how well the corresponding population values can be estimated. For example, a comparison shows that the sample mean is more statistically efficient than the sample median when the data is uncontaminated by data from heavily-tailed data distribution or from mixtures of data distribution, but less efficient otherwise and that the efficiency of the sample median is higher than that for a wide range of distributions. To be more specific, the median has 64% efficiency compared to minimum-variance-mean ( for large normal samples ).

Last Updated on November 13, 2021 by admin

## Python | Pandas Dataframe.sort_values()Python | Pandas Dataframe.sort_values()

Pandas Dataframe.sort_values()   Pandas sort_values() function sorts a data frame in Ascending or Descending order of passed Column. It’s different than the sorted Python function since it cannot sort a data frame and particular column cannot be selected. Let’s discuss

## How to convert Float to Int in Python?How to convert Float to Int in Python?

How to convert Float to Int in Python? Converting a float value to an int is done by Type conversion, which is an explicit method of converting an operand to a specific type. However, it is to be noted that such type of conversion may

## Python – random.sample() functionPython – random.sample() function

Python | random.sample() function sample() is an inbuilt function of random module in Python that returns a particular length list of items chosen from the sequence i.e. list, tuple, string or set. Used for random sampling without replacement. Syntax : random.sample(sequence, k) Parameters: sequence:

## Pandas DataFrame reset_index() methodPandas DataFrame reset_index() method

Pandas DataFrame reset_index() Method with Exmaples Pandas reset_index() method is a powerful tool to reset the index of a DataFrame. This method sets a list of integers ranging from 0 to the length of the data as the index. In

## Remove multiple elements from a Python listRemove multiple elements from a Python list

Given a list of numbers, write a Python program to remove multiple elements from a list based on the given condition. Example: Input: [12, 15, 3, 10] Output: Remove = [12, 3], New_List = [15, 10] Input: [11, 5, 17,

## Difference of two columns in Pandas dataframeDifference of two columns in Pandas dataframe

Difference of two columns in Pandas dataframe   Difference of two columns in pandas dataframe in Python is carried out by using following methods : Method #1 : Using ” -” operator. import pandas as pd   # Create a

## Response Methods – Python requestsResponse Methods – Python requests

Response Methods – Python requests When one makes a request to a URI, it returns a response. This Response object in terms of python is returned by requests.method(), method being – get, post, put, etc. Response is a powerful object

## Python DestructorsPython Destructors

Destructors in Python Destructors are called when an object gets destroyed. In Python, destructors are not needed as much needed in C++ because Python has a garbage collector that handles memory management automatically. The __del__() method is a known as a destructor