Python:Pandas .median()

Abhayp2004's avatar
Published Oct 13, 2025
Contribute to Docs

The .median() method returns a Series or DataFrame containing the median of each group in a GroupBy object, ignoring NaN values by default.

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

groupbyobject.median(numeric_only)

Parameters:

  • numeric_only: Boolean value. If True, includes only numeric columns. Default is False.

Return value:

Returns a new Series or DataFrame containing the median values for each group.

Example

The following example produces a GroupBy object from a DataFrame and executes the .median() method on it:

import pandas as pd
df = pd.DataFrame({'Team' : ['A', 'A', 'A', 'B', 'B', 'C'],
'Score' : [10, 20, 30, 40, 50, 60]})
print(df, end='\n\n')
group = df.groupby(['Team'], as_index=False)
print(group.median())

This example produces the following output:

Team Score
0 A 10
1 A 20
2 A 30
3 B 40
4 B 50
5 C 60
Team Score
0 A 20.0
1 B 45.0
2 C 60.0

Codebyte Example

In this example, median scores of students in different classes are computed to analyze performance:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python:Pandas on Codecademy

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours