Sub topic 7
Choosing your own percentiles
This chapter assumes you can build a DataFrame and have used describe() with its default output. It covers the percentiles argument: what it accepts, how the row labels work, and the errors you are most likely to hit.
What the default shows
By default, describe() reports the 25%, 50%, and 75% percentiles of each numeric column. A percentile is the value below which a given share of the data falls. The 25% value is the first quartile, 50% is the median, and 75% is the third quartile.
Those three values describe the middle of the data. They say little about the tails, where outliers and data-entry mistakes tend to sit. The percentiles argument lets you ask for values nearer the extremes, such as 5% and 95%.
Passing a list of fractions
Give percentiles a list of numbers between 0 and 1. The following code uses the sample DataFrame from earlier in the tutorial.
import numpy as np
import pandas as pd
df = pd.DataFrame({
"name": ["Ana", "Ben", "Cleo", "Dev", "Eli"],
"age": [34, 28, np.nan, 45, 39],
"city": ["Lima", "Oslo", "Lima", "Pune", "Oslo"],
"salary": [52000, 48000, 61000, 75000, 58000],
})
print(df.describe(percentiles=[0.1, 0.9]))The output rows are count, mean, std, min, 10%, 50%, 90%, and max. Your list replaces the default 25% and 75% rows. The 50% row stays.
pandas computes each percentile by linear interpolation between the two nearest sorted values. Here are the three percentile rows for the numeric columns:
| Row | age | salary |
|---|---|---|
| 10% | 29.8 | 49600.0 |
| 50% | 36.5 | 58000.0 |
| 90% | 43.2 | 69400.0 |
For age, the four non-missing values sorted are 28, 34, 39, and 45. The 10% position falls 0.3 of the way from 28 to 34, which gives 29.8. The missing age is skipped, as it is in every describe statistic.
How the row labels are built
pandas turns each fraction into a percent label. The fraction 0.1 becomes 10%. Fractions that are not whole percents keep the decimals they need, so 0.001 becomes 0.1% and 0.995 becomes 99.5%.
print(df["salary"].describe(percentiles=[0.01, 0.99]))
print(df["salary"].describe(percentiles=[]))The second call passes an empty list. You still get a 50% row, because describe always includes the median. You cannot remove it.
Because describe sorts the list, the order you pass does not matter. [0.9, 0.1] gives the same rows as [0.1, 0.9].
Common errors
Most mistakes come from one of three things.
- Using percent numbers instead of fractions.
percentiles=[10, 90]raises aValueErrorsaying the values should be in the interval [0, 1]. Write0.1and0.9. - Repeating a value.
percentiles=[0.5, 0.5]raises aValueErrorbecause the list cannot contain duplicates. Passing[0.25, 0.5]is fine, but[0.5]and the automatic median do not conflict, since pandas merges them. - Expecting percentiles on text columns. The percentile rows describe numeric data only. With
include="all", text columns showNaNin those rows, because a percentile has no meaning for text.
When to use quantile instead
If you only need a few percentiles and no other statistics, quantile is more direct. It returns just the requested values.
print(df[["age", "salary"]].quantile([0.1, 0.9]))The numbers match the describe output because both use linear interpolation. Choose describe when you also want count, mean, and spread in the same view. A useful habit is to request 5% and 95% when you suspect outliers. If the 95% value is close to the maximum, the top of the data is tight. If the maximum is many times larger than the 95% value, look at the largest rows before you trust any average.
Exercise
You run df.describe(percentiles=[5, 95]) and get a ValueError. What is wrong, and what is the corrected call? Which rows appear in the corrected output for a numeric column?
Show answer
The percentiles must be fractions between 0 and 1, not percent numbers. The corrected call is df.describe(percentiles=[0.05, 0.95]). The rows are count, mean, std, min, 5%, 50%, 95%, and max. The 50% row appears even though it was not requested.