Sub topic 6
Summarizing numeric columns with describe
This chapter assumes you know basic Python syntax, have pandas imported as pd, and have already used head and info to look at a DataFrame. Here you will use describe to see how the values in each numeric column are spread out.
What describe does
DataFrame.describe() computes a set of summary statistics for every numeric column and returns them as a new DataFrame. It answers a different question from info. The info method tells you what is stored. describe tells you what the values look like.
We will use the same DataFrame as earlier. One age is missing on purpose.
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().round(2))By default, only numeric columns appear, so name and city are left out. The output is:
age salary
count 4.00 5.00
mean 36.50 58800.00
std 7.23 10377.86
min 28.00 48000.00
25% 32.50 52000.00
50% 36.50 58000.00
75% 40.50 61000.00
max 45.00 75000.00The .round(2) call only makes the printout shorter. It does not change what describe computes.
Reading each row
| Row | Meaning |
|---|---|
| count | Number of non-missing values |
| mean | Arithmetic average |
| std | Sample standard deviation, a measure of spread |
| min | Smallest value |
| 25% | First quartile: a quarter of the values fall below it |
| 50% | Median: the middle value |
| 75% | Third quartile: three quarters of the values fall below it |
| max | Largest value |
Each of these is a quick check on the data.
- count is where missing data shows up.
agehas a count of 4 andsalaryhas 5, so one age is missing. The table has 5 rows, socountbelow the row count always means gaps. - std uses
ddof=1, which divides by n − 1. This is the same default asSeries.std()and differs from NumPy'snp.std, which divides by n. - Quartiles are computed with linear interpolation. For
age, the sorted values are 28, 34, 39, 45. The 25% mark sits three quarters of the way between 28 and 34, which gives 32.5. That value does not appear in the data, and this is normal.
Comparing mean and median
The gap between mean and 50% is a quick way to check for skew. In salary, the mean (58,800) is a little above the median (58,000). The 75,000 value pulls the mean upward. If one salary were 750,000, the mean would jump while the median barely moved.
Also compare min and max with what you expect. An age of 250 or a negative salary will stand out in these two rows even when the rest of the table looks normal.
Choosing your own percentiles
The percentiles argument takes a list of fractions between 0 and 1.
print(df.describe(percentiles=[0.1, 0.9]).round(2))The rows between min and max are now 10%, 50%, and 90%. The median is always included, even if you leave it out of the list. This is useful when you care about the tails. For example, [0.01, 0.99] shows how far the extreme 1% of values reach.
Selecting a single column
Calling describe on one column returns a Series with the same row labels.
print(df["age"].describe())
print(df["salary"].describe()["50%"])Because the result is an ordinary Series, you can pull out one statistic with its label, as the second line does.
Common mistakes
- Expecting every column to appear. If a numeric column is missing from the output, its dtype is probably
objectbecause numbers were stored as text. Checkdf.dtypesorinfo. - Reading count as the row count. It is the non-missing count per column, so the columns can differ.
- Trusting the mean alone. One extreme value can distort it. Read it next to the median.
- Forgetting that the output is a DataFrame. You can transpose it with
df.describe().Tto get one row per column, which is easier to read when there are many columns.
Exercise
A DataFrame has 1,000 rows. describe() shows count = 940 for the column "income", a mean of 52,000, and a median (50%) of 41,000. What do these numbers suggest about the column?
Show answer
The count of 940 means 60 values are missing, and they are left out of every statistic. The mean is well above the median, which suggests a few very large incomes are pulling the average up. Check max and the upper percentiles for outliers before relying on the mean.
Exercise
Write one line of code that shows the 5th, 50th, and 95th percentiles of the "salary" column only.
Show answer
print(df["salary"].describe(percentiles=[0.05, 0.95])) shows the 5% and 95% rows, and the 50% row is added automatically.