how to calculate mode and median in pandas DataFrame with examples
In this pandas tutorial, we will discuss about:
-
how to calculate mode in pandas?
-
mode in pandas example
-
how to calculate median in pandas?
-
median in pandas example
Lets begin with how to calculate mode and median in pandas DataFrame. But before that lets understand about dataframe first.
DataFrame is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.
We can able to create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.
Syntax:
pandas.DataFrame(data)
Where, data is the input dataframe, The data can be a dictionary that stores list of values with specified key.
Example: Create dataframe
In this example, we will create a dataframe with 4 rows and 4 columns with building data and assign indices through index parameter.
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'building-id':['c-001','c-021','c-002','c-004'],
'length':[5.6,7.8,4.5,5.3],
"breadth":[12.9,4.5,21.5,6.0],
"area":[20,56,43,45]
},index=['one','two','three','four'])
#display the dataframe
print(data)
Output: Dataframe is created below
building-id length breadth area
one c-001 5.6 12.9 20
two c-021 7.8 4.5 56
three c-002 4.5 21.5 43
four c-004 5.3 6.0 45
Now lets use this dataframe to understand how to calculate mode in pandas and how to calculate median in pandas?
calculate mode in pandas
We can mode in pandas dataframe by using mode() function.
Syntax:
dataframe.mode(axis)
where, dataframe is the input dataframe
-
axis =1 represents column, which will return the most repated value value (mode) column wise.
-
axis= 0 represents row, which will return the most repated value value (mode) row wise.
Lets see few mode in pandas example.
Example 1: calculate mode in pandas
Lets calculate mode in pandas across column
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'building-id':['c-001','c-021','c-002','c-004'],
'length':[5.6,7.8,4.5,5.3],
"breadth":[12.9,4.5,21.5,6.0],
"area":[20,56,43,45]
},index=['one','two','three','four'])
#get the mode
print(data.mode(axis=1))
Output: Result for calculate mode in pandas is given below
0 1 2 3
one c-001 5.6 12.9 20
two c-021 7.8 4.5 56
three c-002 4.5 21.5 43
four c-004 5.3 6.0 45
Example 2: calculate mode in pandas
Lets calculate mode in pandas across row.
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'building-id':['c-001','c-021','c-002','c-004'],
'length':[5.6,7.8,4.5,5.3],
"breadth":[12.9,4.5,21.5,6.0],
"area":[20,56,43,45]
},index=['one','two','three','four'])
#get the mode
print(data.mode(axis=0))
Output: Result for calculate mode in pandas is given below
building-id length breadth area
0 c-001 4.5 4.5 20
1 c-002 5.3 6.0 43
2 c-004 5.6 12.9 45
3 c-021 7.8 21.5 56
Note - we can specify column name, if we want to return mode for particular column.
Example 3: calculate mode in pandas
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'building-id':['c-001','c-021','c-002','c-004'],
'length':[5.6,7.8,4.5,5.3],
"breadth":[12.9,4.5,21.5,6.0],
"area":[20,56,43,45]
},index=['one','two','three','four'])
#get the mode for only length column
print(data['length'].mode())
Output: Result for calculate mode in pandas is given below
0 4.5
1 5.3
2 5.6
3 7.8
dtype: float64
We have seen several example on how to calculate mode in pandas? Lets see median in pandas example.
calculate median in pandas
We can median in pandas dataframe by using median() function.
Syntax:
dataframe.median(axis)
where, dataframe is the input dataframe
-
axis =1 represents column, which will return the median column wise.
-
axis= 0 represents row, which will return the median row wise.
Lets see few median in pandas example.
Example 1: calculate median in pandas
Lets calculate median in pandas across column
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'building-id':['c-001','c-021','c-002','c-004'],
'length':[5.6,7.8,4.5,5.3],
"breadth":[12.9,4.5,21.5,6.0],
"area":[20,56,43,45]
},index=['one','two','three','four'])
#calculate median in pandas
print(data.median(axis=1))
Output: Result for calculate median in pandas is given below
one 12.9
two 7.8
three 21.5
four 6.0
dtype: float64
Example 2: calculate median in pandas
Lets calculate median in pandas across row.
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'building-id':['c-001','c-021','c-002','c-004'],
'length':[5.6,7.8,4.5,5.3],
"breadth":[12.9,4.5,21.5,6.0],
"area":[20,56,43,45]
},index=['one','two','three','four'])
#calculate median in pandas
print(data.median(axis=0))
Output: calculate median in pandas result
length 5.45
breadth 9.45
area 44.00
dtype: float64
Note - we can specify column name, if we want to return median for particular column.
Example 3: calculate median in pandas
Lets calculate median in pandas by specifying column name
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'building-id':['c-001','c-021','c-002','c-004'],
'length':[5.6,7.8,4.5,5.3],
"breadth":[12.9,4.5,21.5,6.0],
"area":[20,56,43,45]
},index=['one','two','three','four'])
#get the median for only length column
print(data['length'].median())
Output: Result for calculate median in pandas
5.449999999999999
Thus we have completed our session on how to calculate mode in pandas, how to calculate median in pandas with various examples.
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1200+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Apr 03,2022