- Python statistics.median() Method
- Definition and Usage
- Syntax
- Parameter Values
- Technical Details
- How to Calculate Median in Python (with Examples)
- What Is the Median Value in Maths
- Why and When Is Median Value Useful
- How to Calculate the Median Value in Python
- How to Implement Median Function in Python
- How to Use a Built-In Median Function in Python
- Conclusion
- Further Reading
- Python – Get median of a List
- What is median?
- Median of a Python List
- 1. From scratch implementation of median in Python
- 2. Using statistics library
- 3. Using numpy library
- Author
Python statistics.median() Method
Calculate the median (middle value) of the given data:
# Import statistics Library
import statistics
# Calculate middle values
print(statistics.median([1, 3, 5, 7, 9, 11, 13]))
print(statistics.median([1, 3, 5, 7, 9, 11]))
print(statistics.median([-11, 5.5, -3.4, 7.1, -9, 22]))
Definition and Usage
The statistics.median() method calculates the median (middle value) of the given data set. This method also sorts the data in ascending order before calculating the median.
Tip: The mathematical formula for Median is: Median = <(n + 1) / 2>th value, where n is the number of values in a set of data. In order to calculate the median, the data must first be sorted in ascending order. The median is the number in the middle.
Note: If the number of data values is odd, it returns the exact middle value. If the number of data values is even, it returns the average of the two middle values.
Syntax
Parameter Values
Parameter | Description |
---|---|
data | Required. The data values to be used (can be any sequence, list or iterator) |
Note: If data is empty, it returns a StatisticsError.
Technical Details
Return Value: | A float value, representing the median (middle value) of the given data |
---|---|
Python Version: | 3.4 |
How to Calculate Median in Python (with Examples)
For example, let’s calculate the median of a list of numbers:
import statistics numbers = [1, 2, 3, 4, 5, 6, 7] med = statistics.median(numbers) print(med)
The median value is a common way to measure the “centrality” of a dataset.
If you are looking for a quick answer, I’m sure the above example will do. But to really learn what median really is, why it is useful, and how to find it, read along.
This is a comprehensive guide to finding the median in Python.
What Is the Median Value in Maths
The Median is the middle value of a given dataset.
If you have a list of 3 numbers, the median is the second number as it is in the middle.
But in case you have a list of 4 values, there is no “middle value”. When calculating the median, of an even-sized dataset, the average of the two middle values is used.
Why and When Is Median Value Useful
When dealing with statistics, you usually want to have a single number that describes the nature of a dataset.
Think about your school grades for example. Instead of seeing the dozens of grades, you want to know the average (the mean).
Usually, measuring the “centrality” of a dataset means calculating the mean value. But if you have a skewed distribution, the mean value can be unintuitive.
Let’s say you drive to your nearby shopping mall 7 times. Usually, the drive takes around 10 minutes. But one day the traffic jam makes it last 2 hours.
Here is a list of driving times to the mall:
[9, 120, 10, 9, 10, 10, 10]
Now if you take the average of this list, you get ~25 minutes. But how well does this number really describe your trip?
As you can see, most of the time the trip takes around 10 minutes.
To better describe the driving time, you should use a median value instead. To calculate the median value, you need to sort the driving times first:
[9, 9, 10, 10, 10, 10, 120]
Then you can choose the middle value, which in this case is 10 minutes. 10 minutes describes your typical trip length way better than 25, right?
The usefulness of calculating the median, in this case, is that the unusually high value of 120 does not matter.
In short, you can calculate the median value when measuring centrality with average is unintuitive.
How to Calculate the Median Value in Python
In Python, you can either create a function that calculates the median or use existing functionality.
How to Implement Median Function in Python
If you want to implement the median function, you need to understand the procedure of finding the median.
The median function works such that it:
- Takes a dataset as input.
- Sorts the dataset.
- Checks if the dataset is odd/even in length.
- If the dataset is odd in length, the function picks the mid-value and returns it.
- If the dataset is even, the function picks the two mid values, calculates the average, and returns the result.
Here is how it looks in the code:
def median(data): sorted_data = sorted(data) data_len = len(sorted_data) middle = (data_len - 1) // 2 if middle % 2: return sorted_data[middle] else: return (sorted_data[middle] + sorted_data[middle + 1]) / 2.0
numbers = [1, 2, 3, 4, 5, 6, 7] med = median(numbers) print(med)
Now, this is a valid approach if you need to write the median function yourself. But with common maths operations, you should use a built-in function to save time and headaches.
Let’s next take a look at how to calculate the median with a built-in function in Python.
How to Use a Built-In Median Function in Python
In Python, there is a module called statistics. This module contains useful mathematical tools for data science and statistics.
One of the great methods of this module is the median() function.
As the name suggests, this function calculates the median of a given dataset.
To use the median function from the statistics module, remember to import it into your project.
Here is an example of calculating the median for a bunch of numbers:
import statistics numbers = [1, 2, 3, 4, 5, 6, 7] med = statistics.median(numbers) print(med)
Conclusion
Today you learned how to calculate the median value in Python.
To recap, the median value is a way to measure the centrality of a dataset. The Median is useful when the average doesn’t properly describe the dataset and gives falsy results.
To calculate the median in Python, use the built-in median() function from the statistics module.
import statistics numbers = [1, 2, 3, 4, 5, 6, 7] med = statistics.median(numbers)
Thanks for reading. Happy coding!
Further Reading
Python – Get median of a List
In this tutorial, we will look at how to get the median value of a list of values in Python. We will walk you through the usage of the different methods with the help of examples.
What is median?
Median is a descriptive statistic that is used as a measure of central tendency of a distribution. It is equal to the middle value of the distribution. There are equal number of values smaller and larger than the median. It is also not much sensitive to the presence of outliers in the data like the mean (another measure of central tendency).
📚 Discover Online Data Science Courses & Programs (Enroll for Free)
Introductory ⭐
Intermediate ⭐⭐⭐
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
To calculate the median of a list of values –
- Sort the values in ascending or descending order (either works).
- If the number of values, n, is odd, then the median is the value in the (n+1)/2 position in the sorted list(or array) of values.
If the number of values, n, is even, then the median is the average of the values in n/2 and n/2 + 1 position in the sorted list(or array) of values.
For example, calculate the median of the following values –
First, let’s sort these numbers in ascending order.
Now, since the total number of values is even (8), the median is the average of the 4th and the 5th value.
Thus, median comes out to be 3.5
Now that we have seen how is the median mathematically calculated, let’s look at how to compute the median in Python.
Median of a Python List
To compute the median of a list of values in Python, you can write your own function, or use methods available in libraries like numpy , statistics , etc. Let’s look at these methods with the help of examples.
1. From scratch implementation of median in Python
You can write your own function in Python to compute the median of a list.
def get_median(ls): # sort the list ls_sorted = ls.sort() # find the median if len(ls) % 2 != 0: # total number of values are odd # subtract 1 since indexing starts at 0 m = int((len(ls)+1)/2 - 1) return ls[m] else: m1 = int(len(ls)/2 - 1) m2 = int(len(ls)/2) return (ls[m1]+ls[m2])/2 # create a list ls = [3, 1, 4, 9, 2, 5, 3, 6] # get the median print(get_median(ls))
Here, we use the list sort() function to sort the list, and then depending upon the length of the list return the median. We get 3.5 as the median, the same we manually calculated above.
Note that, compared to the above function, the libraries you’ll see next are better optimized to compute the median of a list of values.
2. Using statistics library
You can also use the statistics standard library in Python to get the median of a list. Pass the list as argument to the statistics.median() function.
import statistics # create a list ls = [3, 1, 4, 9, 2, 5, 3, 6] # get the median print(statistics.median(ls))
We get the same results as above.
For more on the statistics library in Python, refer to its documentation.
3. Using numpy library
The numpy library’s median() function is generally used to calculate the median of a numpy array. You can also use this function on a Python list.
import numpy as np # create a list ls = [3, 1, 4, 9, 2, 5, 3, 6] print(np.median(ls))
You can see that we get the same result.
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Author
Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects. View all posts
Data Science Parichay is an educational website offering easy-to-understand tutorials on topics in Data Science with the help of clear and fun examples.