# 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 |

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 |

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 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 |

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 |

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:

**Please accept YouTube cookies to play this video.** By accepting you will be accessing content from YouTube, a service provided by an external third party.

If you accept this notice, your choice will be saved and the page will refresh.

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

- Calculate Median in Python
- Get Median of Array with np.median Function of NumPy Library
- median Function of statistics Module
- Calculate Mean in Python
- Calculate Mode in Python
- Introduction to the pandas Library in Python
- Python Programming Language

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.