How to calculate 1st and 3rd quartiles in pandas dataframe?

Learn how to calculate 1st and 3rd quartiles in pandas dataframe? By Pranit Sharma Last updated : October 06, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Calculating 1st and 3rd quartiles

The quantiles are usually divided into a subgroup of 25%, 50%, and 75%.

Pandas have a method called quantile() which takes a list of all the quantiles we want as an argument. We pass the quantiles in decimal form, for instance, 25% will be passed as 0.25.

Let us understand with the help of an example,

Python program to calculate 1st and 3rd quartiles in pandas dataframe

# Importing pandas package
import pandas as pd

# Creating a Dictionary
data = data = {
    'A':[6,4,5,7,4],
    'B':[7,4,7,9,2]
}

# Creating a DataFrame
df = pd.DataFrame(data)

# Display DataFrame
print("Original DataFrame:\n",df,"\n")

# Calculating quantiles
result = df.A.quantile([0.25,0.5,0.75])

# Display result
print("Result:\n",result)

Output

The output of the above program is:

Example: How to calculate 1st and 3rd quartiles in pandas dataframe?frame

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.