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
- Introduction
statistics.median
Function Syntax- Examples
- Basic Usage
- Median of a List of Numbers
- Median of a Tuple of Numbers
- Handling Odd and Even Number of Elements
- Real-World Use Case
- 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
: Ifdata
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
Post a Comment
Leave Comment