Median Function in Python pandas (Dataframe, Row and column wise median)

median() – Median Function in python pandas is used to calculate the median or middle value of a given set of numbers, Median of a data frame, median of column and median of rows, let’s see an example of each. We need to use the package name “statistics” in calculation of median. In this tutorial we will learn,

We need to use the package name “statistics” in calculation of median. In this tutorial we will learn,

  • How to find the median of a given set of numbers
  • How to find median of a dataframe
  • How to find the median of a column in dataframe
  • How to find row median of a dataframe

Median Function in Python

Simple median function is shown below


# calculate median or middle value
Import statistics

print(statistics.median([1,9,5,6,8,7]))
print(statistics.median([4,-11,-5,16,5,7,9]))

output:

6.5
5

 

Median of a dataframe:

Create dataframe

import pandas as pd
import numpy as np

#Create a DataFrame
d = {
    'Name':['Alisa','Bobby','Cathrine','Madonna','Rocky','Sebastian','Jaqluine',
   'Rahul','David','Andrew','Ajay','Teresa'],
   'Score1':[62,47,55,74,31,77,85,63,42,32,71,57],
   'Score2':[89,87,67,55,47,72,76,79,44,92,99,69],
   'Score3':[56,86,77,45,73,62,74,89,71,67,97,68]}

df = pd.DataFrame(d)
df

So the resultant dataframe will be

median function in python pandas

 

Median of the dataframe:


# median of the dataframe
df.median()

will calculate the median of the dataframe across columns so the output will

Score1   59.5
Score2   74.0
Score3   72.0
dtype: float64

 

Column Median of the dataframe:


# column median of the dataframe
df.median(axis=0)

axis=0 argument calculates the column wise median of the dataframe so the result will be

0   62.0
1   86.0
2   67.0
3   55.0
4   47.0
5   72.0
6   76.0
7   79.0
8   44.0
9   67.0
10   97.0
11   68.0
dtype: float64

 

Calculate the median of the specific Column


# median of the specific column
df.loc[:,"Score1"].median()

the above code calculates the median of the “Score1” column so the result will be

59.5

 

previous-small median function in python                                                                                                           next_small median function in python

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts