Python statistics median_grouped()

The statistics.median_grouped function in Python's statistics module calculates the median of grouped continuous data, using interpolation. This function is useful for finding the median in datasets where data is grouped into bins or intervals.

Table of Contents

  1. Introduction
  2. statistics.median_grouped Function Syntax
  3. Examples
    • Basic Usage
    • Median of Grouped Data with Default Interval
    • Median of Grouped Data with Specified Interval
  4. Real-World Use Case
  5. Conclusion

Introduction

The statistics.median_grouped function is part of the statistics module, which provides functions for mathematical statistics of numeric data. This function is useful for datasets where data is grouped into intervals, and you want to estimate the median using interpolation.

statistics.median_grouped Function Syntax

Here's how you use the statistics.median_grouped function:

import statistics

median_value = statistics.median_grouped(data, interval=1)

Parameters:

  • data: A sequence or iterable of numeric data (list, tuple, etc.).
  • interval: Optional. The class interval. Default is 1.

Returns:

  • The median of the grouped data.

Raises:

  • StatisticsError: If data is empty.

Examples

Basic Usage

Calculate the median of grouped data with default interval.

import statistics

data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
median_value = statistics.median_grouped(data)
print(f"Median (grouped): {median_value}")

Output:

Median (grouped): 3.875

Median of Grouped Data with Default Interval

Calculate the median of grouped data using the default interval (1).

import statistics

numbers = [1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5]
median_value = statistics.median_grouped(numbers)
print(f"Median of grouped data: {median_value}")

Output:

Median of grouped data: 3.6666666666666665

Median of Grouped Data with Specified Interval

Calculate the median of grouped data using a specified interval.

import statistics

numbers = [1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5]
median_value = statistics.median_grouped(numbers, interval=2)
print(f"Median of grouped data with interval 2: {median_value}")

Output:

Median of grouped data with interval 2: 3.3333333333333335

Real-World Use Case

Calculating the Median Income from Grouped Data

Estimate the median income from a dataset where income is grouped into intervals.

import statistics

incomes = [25000, 30000, 30000, 35000, 35000, 35000, 40000, 40000, 40000, 40000, 45000, 45000, 45000, 45000, 45000]
median_income = statistics.median_grouped(incomes, interval=5000)
print(f"Median income (grouped): {median_income}")

Output:

Median income (grouped): 39375.0

Conclusion

The statistics.median_grouped function is used for calculating the median of grouped continuous data in Python. It is particularly helpful when dealing with data that is divided into intervals or bins, allowing for a more accurate estimation of the median using interpolation. This function is beneficial in various real-world scenarios, such as estimating the median from grouped income data.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare