Calculate Median by Group in Python (2 Examples)

 

In this Python programming tutorial you’ll learn how to compute the median by group.

The content of the tutorial looks like this:

Let’s dive right in:

 

Example Data & Software Libraries

We first need to load the pandas library:

import pandas as pd                                        # Load pandas library

Let’s also create some example data in Python:

data = pd.DataFrame({'x1':[6, 5, 2, 2, 5, 1, 5, 6, 1, 8],  # Create pandas DataFrame
                     'x2':range(9, 19),
                     'group1':['A', 'B', 'B', 'A', 'C', 'A', 'C', 'B', 'B', 'A'],
                     'group2':['a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b']})
print(data)                                                # Print pandas DataFrame

 

table 1 DataFrame calculate median group python programming language

 

Table 1 visualizes the structure of the example pandas DataFrame – It consists of ten rows and four columns. The variables x1 and x2 are floats and the variables group1 and group2 are are group and subgroup indicators.

 

Example 1: Median by Group in pandas DataFrame

In this example, I’ll illustrate how to calculate the median by group based on one group column.

For this task, we have to apply the groupby and median functions as shown below:

print(data.groupby('group1').median())                     # Get median by group
#          x1    x2
# group1           
# A       4.0  13.0
# B       3.5  13.5
# C       5.0  14.0

The previous output shows six median values – one for each group in each of the two float columns.

 

Example 2: Median by Group & Subgroup in pandas DataFrame

We can also specify multiple group columns to define main and subgroups.

Consider the Python syntax below:

print(data.groupby(['group1', 'group2']).median())         # Get median by multiple groups
#                 x1    x2
# group1 group2           
# A      a       2.0  12.0
#        b       8.0  18.0
# B      a       3.5  10.5
#        b       3.5  16.5
# C      a       5.0  13.0
#        b       5.0  15.0

 

Video & Further Resources

Some time ago, I have published a video on the Statistics Globe YouTube channel, which shows the topics of this article. You can find the video below:

 

The YouTube video will be added soon.

 

Additionally, you might want to read some of the other tutorials on my website. You can find some articles below:

 

You have learned in this article how to calculate and find the median value by group in Python programming. If you have further questions, please let me know in the comments section. Furthermore, please subscribe to my email newsletter to receive updates on the newest articles.

 

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.

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

Menu
Top