To find the medians of the columns in a DataFrame, or the median value of a Series in pandas, the easiest way is to use the pandas median() function.


You can also use the numpy median() function.


When working with data, many times we want to calculate summary statistics to understand our data better. One such statistic is the median, or the middle number of a variable.

Finding the median in a column, or the median for all columns or rows in a DataFrame using pandas is easy. We can use the pandas median() function to find the median value of a column of numbers, or a DataFrame.

Let’s say we have the following DataFrame.

df = pd.DataFrame({'Age': [43,23,71,49,52,37], 

# Output: 
   Age  Test_Score
0   43          90
1   23          87
2   71          92
3   49          96
4   52          84
5   37          79

To get the medians for all columns, we can call the pandas median() function.


# Output:
Age           46.0
Test_Score    88.5
dtype: float64

If we only want to get the median of one column, we can do this using the pandas median() function in the following Python code:


# Output:

This is the same output as if we called the pandas quantile() function for the 50th percentile:


# Output:

Using numpy median to Calculate Medians in pandas DataFrame

We can also use the numpy median() function to calculate the median value of the numbers in a column in a pandas DataFrame.

To get the median of the numbers in the column “Test_Score”, we can use the numpy median() function in the following Python code:


# Output:

As you can see above, this is the same value we received from the pandas median() function.

Hopefully this article has been helpful for you to understand how to find the median value of numbers in a Series or DataFrame in pandas.

Categorized in:


Last Update: February 26, 2024