Calculate Median in Python (5 Examples)

 

In this tutorial, I’ll illustrate how to calculate the median value for a list or the columns of a pandas DataFrame in Python programming.

The page is structured as follows:

Let’s dive right in…

 

Example 1: Median of List Object

This example explains how to get the median value of a list object in Python.

First, we have to create an example list:

my_list = [1, 4, 3, 2, 1, 3, 7, 1, 4, 1]                      # Create example list
print(my_list)                                                # Print example list
# [1, 4, 3, 2, 1, 3, 7, 1, 4, 1]

Furthermore, we have to load the NumPy library:

import numpy as np                                            # Load NumPy library

Next, we can apply the median function of the NumPy library to our example list:

print(np.median(my_list))                                     # Get median of list
# 2.5

As you can see based on the previous output, the median of our list is 2.5.

 

Example 2: Median of One Particular Column in pandas DataFrame

In Example 2, I’ll illustrate how to find the median value for the columns of a pandas DataFrame.

Let’s import pandas to Python:

import pandas as pd                                           # Load pandas library

Next, let’s create an exemplifying pandas DataFrame:

data = pd.DataFrame({'x1':[6, 2, 7, 2, 1, 5, 3, 4, 2, 7, 5],  # Create pandas DataFrame
                     'x2':range(0, 11),
                     'group':['A', 'B', 'B', 'C', 'B', 'A', 'A', 'C', 'C', 'B', 'A']})
print(data)                                                   # Print pandas DataFrame

 

table 1 DataFrame calculate median python programming language

 

After running the previous Python programming code the pandas DataFrame you can see in Table 1 has been created. Our example data set contains two float columns and a group indicator.

Next, we can compute the median for one specific column (i.e. x1) as shown below:

print(data['x1'].median())                                    # Get median of one column
# 4.0

The median of the column x1 is equal to 4.0.

 

Example 3: Median of All Columns in pandas DataFrame

This example demonstrates how to return the medians for all columns of our pandas DataFrame.

For this task, we can simply apply the median function to our entire data set:

print(data.median())                                          # Get median of all columns
# x1    4.0
# x2    5.0
# dtype: float64

The median of the column x1 is 4.0 (as we already know from the previous example), and the median of the variable x2 is 5.0.

 

Example 4: Median of Rows in pandas DataFrame

We can also calculate the median of the rows of a pandas DataFrame in Python.

To accomplish this, we have to specify the axis argument within the median function to be equal to 1:

print(data.median(axis = 1))                                  # Get median of rows
# 0     3.0
# 1     1.5
# 2     4.5
# 3     2.5
# 4     2.5
# 5     5.0
# 6     4.5
# 7     5.5
# 8     5.0
# 9     8.0
# 10    7.5
# dtype: float64

 

Example 5: Median by Group in pandas DataFrame

Example 5 shows how to calculate the median for each pandas DataFrame column by group.

For this, we have to use the groupby function in addition to the median function:

print(data.groupby('group').median())                         # Get median by group
#         x1   x2
# group          
# A      5.0  5.5
# B      4.5  3.0
# C      2.0  7.0

The previous output shows the median values for all columns and groups in our data set.

 

Video & Further Resources

In case you need more info on the Python programming code of this article, I recommend watching the following video on my YouTube channel. I demonstrate the contents of this article in the video:

 

 

Besides that, you may want to read the related posts on my website:

 

To summarize: At this point you should have learned how to compute the median value in the Python programming language. In case you have further comments or questions, please let me know in the comments.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top