Python statistics.median_grouped() Method with Examples

statistics.median_grouped() Method in Python:

The statistics.median grouped() method computes the median of grouped continuous data as the 50th percentile.

This method treats the data points as continuous data and computes the 50% percentile median by first determining the median range using the specified interval width (default is 1), and then interpolating within that range using the position of the values from the data set that fall within that range.

GMedian = L + interval * (N / 2 – CF) / F

is the mathematical formula for Grouped Median.

  • L = the lower limit of the median interval
  • interval = the width of the interval
  • N represents the total number of data points.
  • CF = The number of data points that are less than the median interval.
  • The number of data points in the median interval is denoted by F.

Syntax:

statistics.median_grouped(data, interval)

Parameters

data: This is Required. It is the data values that will be used (it can be any sequence, list, or iterator).

interval: This is Optional. The interval between classes. The default value is 1.

Note: It is important to note that if the data is empty, it returns a StatisticsError.

Return Value:

Returns a float value that represents the median of grouped continuous data and is calculated as the 50% percentile.

Examples:

Example1:

Input:

Given list = [10, 20, 40, 15, 30]

Output:

The median of grouped continuous data [10, 20, 40, 15, 30] =  20.0

Example2:

Input:

Given list = [9, 8, 5, 1]
Given interval = 3

Output:

The median of grouped continuous data [9, 8, 5, 1] =  6.5

statistics.median_grouped() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the statistics.median_grouped() method that computes the median of grouped continuous data(given list) as the 50th percentile.
  • Store it in another variable.
  • Print the median of grouped continuous data(given list) as the 50th percentile.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as static input and store it in a variable.
gvn_lst = [10, 20, 40, 15, 30]
# Pass the given list as an argument to the statistics.median_grouped() method
# that computes the median of grouped continuous data(given list) as the 50th
# percentile.
# Store it in another variable.
rslt = statistics.median_grouped(gvn_lst)
# Print the median of grouped continuous data(given list) as the 50th percentile.
print("The median of grouped continuous data", gvn_lst, "= ", rslt)

Output:

The median of grouped continuous data [10, 20, 40, 15, 30] =  20.0
With giving interval

Approach:

  • Import statistics module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Give the interval as static input and store it in another variable.
  • Pass the given list, interval as the arguments to the statistics.median_grouped() method that computes the median of grouped continuous data(given list) as the 50th percentile for the given interval width.
  • Store it in another variable.
  • Print the median of grouped continuous data(given list) as the 50th percentile.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as static input and store it in a variable.
gvn_lst = [9, 8, 5, 1]
# Give the interval as static input and store it in another variable.
gvn_intrvl = 3
# Pass the given list, interval as the arguments to the statistics.median_grouped()
# method that computes the median of grouped continuous data(given list) as
# the 50th percentile for the given interval width.
# Store it in another variable.
rslt = statistics.median_grouped(gvn_lst, gvn_intrvl)
# Print the median of grouped continuous data(given list) as the 50th percentile.
print("The median of grouped continuous data", gvn_lst, "= ", rslt)

Output:

The median of grouped continuous data [9, 8, 5, 1] =  6.5

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Pass the given list as an argument to the statistics.median_grouped() method that computes the median of grouped continuous data(given list) as the 50th percentile.
  • Store it in another variable.
  • Print the median of grouped continuous data(given list) as the 50th percentile.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))

# Pass the given list as an argument to the statistics.median_grouped() method
# that computes the median of grouped continuous data(given list) as the 50th
# percentile.
# Store it in another variable.
rslt = statistics.median_grouped(gvn_lst)
# Print the median of grouped continuous data(given list) as the 50th percentile.
print("The median of grouped continuous data", gvn_lst, "= ", rslt)

Output:

Enter some random List Elements separated by spaces = 5 8 9 4 1
The median of grouped continuous data [5, 8, 9, 4, 1] = 5.0
With giving interval

Approach:

  • Import statistics module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Give the interval as user input using the int(input()) function and store it in another variable.
  • Pass the given list, interval as the arguments to the statistics.median_grouped() method that computes the median of grouped continuous data(given list) as the 50th percentile for the given interval width.
  • Store it in another variable.
  • Print the median of grouped continuous data(given list) as the 50th percentile.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
    
# Give the interval as user input using the int(input()) function and store it in another variable.
gvn_intrvl = int(input("Enter some random number = "))
# Pass the given list, interval as the arguments to the statistics.median_grouped()
# method that computes the median of grouped continuous data(given list) as
# the 50th percentile for the given interval width.
# Store it in another variable.
rslt = statistics.median_grouped(gvn_lst, gvn_intrvl)
# Print the median of grouped continuous data(given list) as the 50th percentile.
print("The median of grouped continuous data", gvn_lst, "= ", rslt)

Output:

Enter some random List Elements separated by spaces = 4 6 7 2
Enter some random number = 5
The median of grouped continuous data [4, 6, 7, 2] = 3.5