Signup/Sign In
PUBLISHED ON: MARCH 26, 2021

Pandas DataFrame quantile() Method

In statistics, quantile referred to as a quantity that divides the dataset into two equal parts. Quartiles, percentiles, and deciles are also quantile that divides the data into four, hundred, and ten equal parts respectively.

In this tutorial, we will discuss and learn the Python pandas DataFrame.quantile() method that returns Series or DataFrame that consists of values at a given quantile over the requested axis. While finding the quantile, this method arranges the data in ascending order and we can use the formula to find the position that is q*(n+1) where q is the quantile and n is the total number of elements.

The below shows the syntax of the DataFrame.quantile() method.

Syntax

DataFrame.quantile(q=0.5, axis=0, numeric_only=True, interpolation='linear')

Parameters

q: It represents the float or array-like, and the default is 0.5 (50% quantile). The value lies between 0 <= q <= 1, the quantile(s) to compute.

axis: It represents index or column axis, '0' for index and '1' for the column and the default is '0'. When the axis=0, function applied over the index axis and when the axis=1 function applied over the column axis.

numeric_only: It represents bool(True or False), the default is True. If the parameter is False, the quantile of datetime and timedelta data will be computed as well.

interpolation: It includes ‘linear’, ‘lower’, ‘higher’, ‘midpoint’, ‘nearest’. This optional parameter specifies the interpolation method to use.

Example: Find the quantile using the DataFrame.quantile() Method

Let's create a DataFrame and find the quantile at 0.5 using the DataFrame.quantile() method. Calculate the position that is 0.5*(4+1)=2.5 which tells that 50% of the data is below 2.5 and 50% of the data is above 2.5.

#importing pandas as pd
import pandas as pd
df=pd.DataFrame({'Age':[12,14,11,12],'Height':[135,140,138,147],'Weight':[35,38,30,45]})
print("---------The DataFrame is---------")
print(df)
print("-----------------")
print(df.quantile(0.5))


---------The DataFrame is---------
Age Height Weight
0 12 135 35
1 14 140 38
2 11 138 30
3 12 147 45
-----------------
Age 12.0
Height 139.0
Weight 36.5
Name: 0.5, dtype: float64

Example 2: Find the quantile using the DataFrame.quantile() Method

Let's create a DataFrame in ascending order and find the quantile at 0.35 using the DataFrame.quantile() method. Calculate the position that is 0.35*(5+1)=2.1 which tells that 35% of the data is below 2.1 and 35% of the data is above 2.5. The quantile method divides the dataset exactly into two equal parts. See the below example.

#importing pandas as pd
import pandas as pd
df=pd.DataFrame({'Age':[11,12,13,14,15],'Height':[135,140,138,147,145],'Weight':[35,38,30,45,45]})
print("---------The DataFrame is---------")
print(df)
print("-----------------")
print(df.quantile(0.35))


---------The DataFrame is---------
Age Height Weight
0 11 135 35
1 12 140 38
2 13 138 30
3 14 147 45
4 15 145 45
-----------------
Age 12.4
Height 138.8
Weight 36.2
Name: 0.35, dtype: float64

Example 3: Find the quantile using the DataFrame.quantile() Method

Let's create a DataFrame and find the quantile at 0.25 and 0.75 by giving values in list or tuple using the DataFrame.quantile() method. The DataFrame.quantile() method returns the values at quantile 0.25 and 0.75. See the below example.

#importing pandas as pd
import pandas as pd
df=pd.DataFrame({'Age':[11,12,13,14,15],'Height':[135,140,138,147,145],'Weight':[35,38,30,45,45]})
print("---------The DataFrame is---------")
print(df)
print("-----------------")
print(df.quantile([0.25,0.75]))


---------The DataFrame is---------
Age Height Weight
0 11 135 35
1 12 140 38
2 13 138 30
3 14 147 45
4 15 145 45
-----------------
Age Height Weight
0.25 12.0 138.0 35.0
0.75 14.0 145.0 45.0

Example: Find the quantile using the DataFrame.quantile() Method

Let's create a DataFrame and find the quantile at 0.5 using the DataFrame.quantile() method over the column axis. See the below example, at index '0', the quantile is 35.0 for three values, at index '1' the quantile is 38.0 for three values, and so on.

#importing pandas as pd
import pandas as pd
df=pd.DataFrame({'Age':[11,12,13,14,15],'Height':[135,140,138,147,145],'Weight':[35,38,30,45,45]})
print(df.quantile(0.5,axis=1))


0 35.0
1 38.0
2 30.0
3 45.0
4 45.0
Name: 0.5, dtype: float64

Conclusion

In this tutorial, we learned the Python pandas DataFrame.quantile() method. We learned the syntax, parameters and applied this method on the DataFrame to understand the DataFrame.quantile() method.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.