+

Python Statistics | median_low ()

The statistics module in Python allows you to use three options for working with median / average elements in a dataset, namely median (), median_low () and median_high () .

Low Median is always a member of the dataset. If the number of data points is odd, the average is returned. When it is even, the lower of the two means is returned. Let`s see how median_low() works.

Syntax: median_low ( [data-set] )

Parameters:
[data-set] : Takes in a list, tuple or an interable set of numeric data.

Returntype: Returns the low median of numeric data. Low median is a member of actual data-set.

Exceptions: StatisticsError is raised when data-set is empty .

Code # 1: Operation

# Python code for demonstration
# works with median_low ()

 
# import statistics module

import statistics

 
# simple list of a set of integers

set1 = [ 1 , 3 , 3 , 4 , 5 , 7 ]

  
# Note: low median will always be
# dataset member.

 
# Display the low median of the dataset

print ( "Low median of the data-set is% s"  

% (statistics.median_low (set1)))

Output:

 Low median of the data-set is 3 

Code # 2: Working with median_low () and median to differentiate between them.

Output:

 Median of the set is 3.5 Low Median of the set is 3 
Code # 3: How median_low () works with different data ranges

# Python code for demonstration
# works with median_low ()

 
# import statistics module

import statistics

 
# simple list of a set of integers

set1 = [ 1 , 3 , 3 , 4 , 5 , 7 ]

  
 # Print dataset median

 
# Average may or may not
# are in the dataset

print ( "Median of the set is% s"  

% (statistics.median (set1)))

  
# Print the low median of the dataset

print ( "Low Median of the set is% s"  

% (statistics.median_low (set1)))

# Python code for demonstration
# works with median_low ()

 
# import statistics module

from statistics import median_low

 
# Import fraction unit as fr

from fractions import Fraction as fr

  
# tuple of positive integers

data1 = ( 2 , 3 , 4 , 5 , 7 , 9 , 11 )

 
# float tuple

data2 = ( 2.4 , 5.1 , 6.7 , 8.9 )

 
# tuple and from a set of fractional numbers

data3 = ( fr ( 1 , 2 ), fr ( 44 , 12 ),

fr ( 10 , 3 ), fr ( 2 , 3 ))

 
# a tuple of negative integers

data4 = ( - 5 , - 1 , - 12 , - 19 , - 3 )

 
# a tuple from a set of positive ones
# and negative integers

data5 = ( - 1 , - 2 , - 3 , - 4 , 4 , 3 , 2 , 1 )

 
# Print the low_median () of the given datasets

print ( "Low Median of data-set 1 is% s" % (median_low (data1)))

print ( "Low Median of data-set 2 is% s" % (median_low (data2)))

print ( " Low Median of data-set 3 is% s " % (median_low (data3)))

print ( "Low Median of data-set 4 is% s" % (median_low (data4)))

print ( "Low Median of data-set 5 is% s" % ( median_low (data5)))

Output:

 Low Median of data-set 1 is 5 Low Median of data-set 2 is 5.1 Low Median of data-set 3 is 2/3 Low Median of data-set 4 is -5 Low Median of data-set 5 is -1 

Code # 4: Increased statistical error

# Code Python for demonstration
# StatisticsError of median_low ()

 
# module import statistics

from statistics import median_low

 
# create an empty set data

empty = []

 
# will raise StatisticsError

print (median_low (empty))

Output:

 Traceback (most recent call last): File "/home/5f3e758236f872d014f9d741743c30a4.py", line 10, in print (median_low (empty)) File "/usr/lib/python3.5/statistics.py", line 376, in median_low raise StatisticsError ("no median for empty data") statistics.StatisticsError: no median for empty data  

Applications:
media n_low () is used when the data is discrete and would prefer the median to be an actual point in the data rather than extrapolated.

Get Solution for free from DataCamp guru