Python:Pandas .median()
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.
Syntax
groupbyobject.median(numeric_only)
Parameters:
numeric_only: Boolean value. IfTrue, includes only numeric columns. Default isFalse.
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 pddf = 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 Score0 A 101 A 202 A 303 B 404 B 505 C 60Team Score0 A 20.01 B 45.02 C 60.0
Codebyte Example
In this example, median scores of students in different classes are computed to analyze performance:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve 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