Python median() A statistics Method

Posted in /  

Python median() A statistics Method
vinaykhatri

Vinay Khatri
Last updated on November 22, 2024

    Python comes with in-built statistics modules that provide many methods for mathematical statistics computations. median() is one of the statistics module methods that can find out the median of a data set. Although Python supports many third-party scientific computational libraries such as numpy, scipy, and pandas that also provide all the statistical methods that can analyze a data set.

    Still, for small projects where you only need to perform a statistic computation on a small data set there, instead of installing powerful data science libraries, we can use the inbuilt statistics module. In this Python tutorial, we will learn about the statistics.median() method, which can find out the median of an unsorted or sorted data-set list.

    Python Median

    In statistics, the median is the middle value from a given ordered or sorted list of data. To find a median value for a list of data, we first need to sort the data in ascending or descending order. Then, the middle number would be the median of the data set.

    For example, for the list of data [1,3,4,5,7], 4 will be the median value. And if the data list has an even number of elements, then the median of that list will be defined by the mean or average of its two middle values. for instance, for the list of data [1,3,4,5,7,8], =(4+5)/2 , = 4.5 will be the median value.

    Python statistics median() method

    The median() method can accept a data set as an iterable object and return the median of the data set. The data set can be a non-empty list or a tuple of integer or floating-point numbers. If we pass string values, it will return TypeError for an even number of data elements, and for an odd number of elements, it may return an incorrect value.

    Syntax

    import statistics
    statistics.median(data)

    Example

    Let's say we have 15 students in a class, and we have an age list that contains the age of all the students. And now, we want to calculate the median age for the class.

    import statistics
    
    # students ages
    ages = [15, 16, 17,14, 13, 12, 13, 15,15,16,17,15,16,16, 15]
    
    # compute the median value
    median_value = statistics.median(ages)
    
    print("The Median age is:", median_value)

    Output

    The Median value is: 15
    

    If we pass an empty value to the median() method, we will encounter the statistics.StatisticsError: no median for empty data Error.

    import statistics
    
    # empty list
    ages = []
    
    # compute the median value
    median_value = statistics.median(ages)
    
    print("The Median age is:", median_value)

    Output

    Traceback (most recent call last):
      File "main.py", line 7, in 
        median_value = statistics.median(ages)
        raise StatisticsError("no median for empty data")
    statistics.StatisticsError: no median for empty data

    Conclusion

    In this Python tutorial, we learned how to use the Python median() method to find the median value for a data set. The median() method accepts an iterable object as an argument and returns the median value for that iterable object. The statistics modules also provide the low_median() and high_median() methods that are similar to the median() method, but return the low and high median value for the data set that has an even number of data elements.

    People are also reading:

    Leave a Comment on this Post

    0 Comments