Using DataFrame.quantile() method in Pandas (5 examples)

Updated: February 22, 2024 By: Guest Contributor Post a comment


Data analysis in Python heavily relies on Pandas, a powerful and flexible data manipulation library. Understanding its capabilities enables you to derive meaningful insights from your data. One such capability is the DataFrame.quantile() method, which is pivotal in statistical analysis. This tutorial walks you through the DataFrame.quantile() method in Pandas, emphasising its application through five incremental examples.

What are Quantiles?

A quantile is a statistical measure that divides a dataset into intervals with equal probability. The most common are quartiles (dividing into quarters), deciles (tenths), and percentiles (hundredths).


Before diving in, ensure you have Python and Pandas installed. If not, you can install Pandas using pip:

pip install pandas

Example 1: Basic Quantile Calculation

In this section, we’ll start with a simple example to compute the median, which is the 50th percentile of a dataset.

import pandas as pd
#Create a simple DataFrame
data = {'Scores': [20, 40, 50, 70, 90]}
df = pd.DataFrame(data)
# Calculate the median
median = df.quantile(0.5)


Scores    50.0
Name: 0.5, dtype: float64

Example 2: Multiple Quantiles

Next, we’ll compute multiple quantiles (25th, 50th, and 75th percentiles, also known as quartiles).

quantiles = df.quantile([0.25, 0.5, 0.75])


0.25    40.0
0.50    50.0
0.75    70.0

Example 3: Quantiles of a Multi-column DataFrame

Here, we extend our example to a DataFrame with multiple columns to demonstrate how quantile() is applied across all numeric columns.

import numpy as np
# Multiple columns DataFrame
data = {'Scores': [20, 40, 50, 70, 90], 'Time': [9, 15, 21, 34, 42]}
df = pd.DataFrame(data)
# Compute quantiles
quantiles = df.quantile([0.25, 0.5, 0.75])


      Scores  Time
0.25    40.0   14.5
0.50    50.0   21.0
0.75    70.0   34.0

Example 4: Conditional Quantiles

Conditional quantiles allow us to focus on a subset of the data. This can be achieved by filtering the DataFrame before applying the quantile method.

# Filter DataFrame and compute quantile for scores above 50
high_scores = df[df['Scores'] > 50].quantile(0.5)


Scores    80.0
Time      38.0
Name: 0.5, dtype: float64

Example 5: Advanced Uses – Quantile Axis

Finally, we delve into a more advanced application by computing quantiles across different axes. This approach is particularly useful in multidimensional data analysis.

# Compute quantiles across columns (axis=1)
column_quantiles = df.quantile(0.5, axis=1)


0    14.5
1    27.5
2    35.5
3    52.0
4    66.0
Name: 0.5, dtype: float64


The DataFrame.quantile() method in Pandas is a powerful tool for statistical analysis, enabling you to compute quantiles for single or multiple columns, apply conditions, and even analyze multidimensional data. By mastering these examples, you can gain deeper insights into your data, making more informed decisions.