Python statistics median()

The statistics.median function in Python's statistics module is used to calculate the median (the middle value) of a given set of numbers. This function is useful for finding the central value of a dataset, especially when the dataset contains outliers or skewed values.

Table of Contents

  1. Introduction
  2. statistics.median Function Syntax
  3. Examples
    • Basic Usage
    • Median of a List of Numbers
    • Median of a Tuple of Numbers
    • Handling Odd and Even Number of Elements
  4. Real-World Use Case
  5. Conclusion

Introduction

The statistics.median function is part of the statistics module, which provides functions for mathematical statistics of numeric data. The median is the middle value in a sorted list of numbers. If the list has an odd number of elements, the median is the middle element. If the list has an even number of elements, the median is the average of the two middle elements.

statistics.median Function Syntax

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

import statistics

median_value = statistics.median(data)

Parameters:

  • data: A sequence or iterable of numeric data (list, tuple, etc.).

Returns:

  • The median of the given data.

Raises:

  • StatisticsError: If data is empty.

Examples

Basic Usage

Calculate the median of a list of numbers.

import statistics

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

Output:

Median: 3

Median of a List of Numbers

Calculate the median of a list of integers.

import statistics

numbers = [10, 20, 30, 40, 50]
median_value = statistics.median(numbers)
print(f"Median of numbers: {median_value}")

Output:

Median of numbers: 30

Median of a Tuple of Numbers

Calculate the median of a tuple of floats.

import statistics

numbers = (1.5, 2.5, 3.5, 4.5, 5.5)
median_value = statistics.median(numbers)
print(f"Median of numbers: {median_value}")

Output:

Median of numbers: 3.5

Handling Odd and Even Number of Elements

Calculate the median of lists with an odd and even number of elements.

Odd Number of Elements

import statistics

numbers = [1, 3, 3, 6, 7, 8, 9]
median_value = statistics.median(numbers)
print(f"Median of numbers: {median_value}")

Output:

Median of numbers: 6

Even Number of Elements

import statistics

numbers = [1, 2, 3, 4, 5, 6, 8, 9]
median_value = statistics.median(numbers)
print(f"Median of numbers: {median_value}")

Output:

Median of numbers: 4.5

Real-World Use Case

Calculating the Median Income

Calculate the median income from a list of incomes.

import statistics

incomes = [30000, 50000, 45000, 60000, 70000, 80000, 55000]
median_income = statistics.median(incomes)
print(f"Median income: {median_income}")

Output:

Median income: 55000

Conclusion

The statistics.median function is a simple and effective way to calculate the median of a set of numbers in Python. It is especially useful for finding the central value of a dataset when the dataset contains outliers or skewed values. This function makes it easy to determine the middle value of a dataset, which is a common requirement in data analysis and statistics.

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