Pandas: Get the median of values in a Series

Updated: February 18, 2024 By: Guest Contributor Post a comment

Introduction

Pandas is a powerful and versatile Python library for data manipulation and analysis which is widely used in data science, machine learning, and many domains of research and development. One of the fundamental tasks in data analysis is summarizing the data’s central tendency, and the median is one of the key metrics used to understand this. In this tutorial, we will explore how to calculate the median of values in a Pandas Series, moving from basic examples to more advanced uses. By the end of this tutorial, you will be well-acquainted with using the median() method effectively within the Pandas library.

Prerequisites

  • Basic understanding of Python programming.
  • Python environment set up with Pandas installed.

Creating a Simple Pandas Series

First, always start by importing the Pandas library. If you have not installed Pandas yet, you can do so by running pip install pandas in your command line.

import pandas as pd

Now, let’s create a simple Pandas Series.

data = pd.Series([2, 5, 3, 8, 4])

Finding the Median

Finding the median of this series is straightforward using the median() method.

Checkout the code below:

median_value = data.median()
print(median_value)

Output: 4.0

This output shows that the median (middle value) of our series is 4.

Handling Even-Length Series

Now, what happens if our series has an even number of values? Let’s see with an example.

data_even = pd.Series([2, 5, 3, 8])

Finding the median:

median_value_even = data_even.median()
print(median_value_even)

Output: 4.0

In the case of an even number of values, Pandas calculates the median as the average of the two middle numbers. Hence, we get 4.0 as before, which is the average of 3 and 5.

Working with NaN Values

What if your series includes NaN (Not a Number) values? Let’s find out.

data_with_nan = pd.Series([2, np.nan, 3, 8, np.nan, 4])

Now, when we calculate the median:

median_with_nan = data_with_nan.median()
print(median_with_nan)

Output: 3.5

Pandas automatically ignores NaN values when calculating the median, thus providing a reliable calculation even in datasets with missing values.

Grouped Data

Sometimes, we need to calculate medians for grouped data. This is where grouping functions combined with the median() method come to play. For our example, let’s use a DataFrame.

data_frame = pd.DataFrame({'Group': ['A', 'B', 'A', 'A', 'B'],
                           'Values': [1, 2, 3, 4, 5]})

Now, let’s group by ‘Group’ and find medians for each:

group_medians = data_frame.groupby('Group').median()
print(group_medians)

Output: A 3.0 B 3.5 Name: Values, dtype: float64

This reveals the median values of 3.0 for Group A and 3.5 for Group B, showcasing the use of median in grouped data analysis.

Applying Weights

In certain situations, you might want to calculate a weighted median. While Pandas does not provide a direct method for this, you can achieve it by replicating each value based on its weight before calculating the median.

For simplicity, let’s consider our original series and a set of weights:

weights = [1, 2, 1, 3, 2]
weighted_data = pd.concat([pd.Series([data.iloc[i]] * weights[i]) for i in range(data.size)]).reset_index(drop=True)

Now, calculating the median of the weighted series:

weighted_median = weighted_data.median()
print(weighted_median)

Output: 4.0

The weighted median, in this case, remains 4, but this method allows you to adjust calculations based on the importance of certain values.

Conclusion

Understanding how to calculate the median of a Pandas Series is crucial for data analysis, providing insights into the central tendency of your data. Through this tutorial, you’ve learned the basic to advanced usage of the median() method, dealing with even-length series, handling NaN values, working with grouped data, and even calculating weighted medians. As you become more comfortable with these techniques, you’ll find them invaluable tools in your data analysis arsenal.