
To calculate median in pandas, we use the dataframe’s median() method. In Statistics, the median is the value that splits the data into half. 50% of the data is above this value and 50% of the data is below this value.
Examples-
Let’s create a dataset to work with.
import pandas as pd
data = {'Apple':[89, 89, 90, 110, 125, 84, 131, 123, 123, 140, 145, 145],
'Orange': [46, 46, 50, 65, 63, 48, 110, 120, 60, 42, 47, 62],
'Banana': [26, 30, 30, 25, 38, 22, 22, 36, 20, 27, 23, 34 ],
'Mango': [80, 80, 90, 125, 130, 150, 140, 140, 135, 135, 80, 90]}
index = ['Jan','Feb','Mar','Apr','May','June','Jul','Aug','Sep','Oct','Nov','Dec']
df = pd.DataFrame(data, index=index)
df

1 . Calculate the median of the columns –
You can calculate the median of a single column like this
df['Apple'].median()
#output
123.0
Or you can calculate the median of all numeric columns like this
df.median()
#output
Apple 123.0
Orange 55.0
Banana 26.5
Mango 127.5
dtype: float64
2 . Calculate the median of the rows –
To calculate the median of the rows, we need to set the axis parameter to axis=1 or columns.
df.median(axis=1)
#output
Jan 63.0
Feb 63.0
Mar 70.0
Apr 87.5
May 94.0
June 66.0
Jul 120.5
Aug 121.5
Sep 91.5
Oct 88.5
Nov 63.5
Dec 76.0
dtype: float64