Pass percentiles to pandas agg() method

Learn, how to pass percentiles to pandas agg() method in Python?
Submitted by Pranit Sharma, on February 12, 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.

Problem statement

Suppose that we are given a pandas dataframe that contains some columns, we need to calculate percentiles for a column and pass them into the agg() method.

Passing percentiles to pandas agg() method

Since we want to aggregate our pandas groupby results using the percentile function, the Python lambda function offers a pretty neat solution but since we would have to calculate the percentiles from another column, it is better that we define some function for calculating percentiles and then calling them with agg() method.

Let us understand with the help of an example,

Python program to pass percentiles to pandas agg() method

# Import pandas
import pandas as pd

# Creating a dataframe
df = pd.DataFrame({'A': ['a','a','b','b'], 'B': [2,0,3,4]})

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

# Defining function for 50th Percentile
def f50(x):
    return x.quantile(0.5)

# Defining function for 90th Percentile
def f90(x):
    return x.quantile(0.9)

# Passing percentile in agg
res = df.groupby(['A']).agg({'B': [f50, f90, 'max']})



The output of the above program is:

Example: Pass percentiles to pandas agg() method

Python Pandas Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.