quantile in pandas in python

Pandas provides the quantile method to compute the quantiles of a Series or DataFrame. The quantile method takes a single argument that specifies the quantile to compute as a decimal (e.g., 0.5 for the median, 0.25 for the first quartile). Here's an example:

main.py
import pandas as pd

# create a DataFrame
data = {'col1': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}
df = pd.DataFrame(data)

# compute the median (quantile of 0.5)
median = df['col1'].quantile(0.5)
print(median)

# compute the first quartile (quantile of 0.25)
first_quartile = df['col1'].quantile(0.25)
print(first_quartile)

# compute the third quartile (quantile of 0.75)
third_quartile = df['col1'].quantile(0.75)
print(third_quartile)
431 chars
18 lines

This example creates a DataFrame with a single column, col1, and then computes the median, first quartile, and third quartile of that column using the quantile method. The results are printed to the console.

related categories

gistlibby LogSnag