Sub topic 7
Summarizing numeric columns
This chapter assumes you can load train.csv into a DataFrame named df, as in the earlier chapters, and that you know what a Series is. You should also know the basic dtypes (int64, float64, object). Here you will summarize the numeric columns and learn to read the summary critically.
Why summarize before plotting
A summary table gives you a column's center, spread, and range in one call. It also catches problems early: impossible values, heavy skew, and columns with many missing entries. You will check these numbers before you draw any charts.
Running describe
DataFrame.describe() summarizes every numeric column by default.
import pandas as pd
df = pd.read_csv("train.csv")
print(df.describe())
print(df.describe(include="object"))The first call returns these rows for each numeric column:
| Row | Meaning |
|---|---|
| count | Number of non-missing values |
| mean | Arithmetic average |
| std | Standard deviation, a measure of spread |
| min, max | Smallest and largest values |
| 25%, 50%, 75% | Quartiles. The 50% row is the median. |
The second call, with include="object", summarizes text columns instead. It reports count, unique, top (the most common value), and freq (how often that value appears).
Reading the output
Start with count. Survived and Pclass should show 891, which is the number of rows. Age shows fewer, because describe skips missing values. So the mean and median of Age describe only the passengers whose age is known. Missing values are a different issue from the spread of the data, and you should keep them in mind when you read any statistic.
Then compare the mean to the median. In Fare, the mean is far above the median. A few very expensive tickets pull the mean upward. This pattern is called right skew. For skewed columns, the median is usually the better description of a typical passenger.
Finally, check min and max. A minimum age below 1 is plausible for an infant, since Titanic ages are fractions of a year for babies. A Fare minimum of 0 is worth a note, because it may mean a free ticket or a missing value coded as zero.
Summarizing selected columns
You can pick columns and statistics yourself with agg.
stats = df[["Age", "Fare"]].agg(["count", "mean", "median", "std", "min", "max"])
print(stats)
print(df["Fare"].quantile([0.5, 0.9, 0.99]))quantile shows how fast a column grows toward its extreme. If the 99th percentile of Fare is far above the 90th, the tail is long, and the maximum is an outlier-like value you should look at rather than delete.
Common errors
- Reading the mean without the count. A mean computed over 714 of 891 rows may not represent the whole group.
- Trusting the mean on skewed data. Compare it to the median first.
- Summarizing codes as quantities. Averages of ID-like or category-like numbers carry no meaning.
- Treating an outlier as an error. Check the row before you remove anything.
Exercise
In df.describe(), the Fare column has a mean of about 32 and a median of about 14. What does this tell you, and which value would you report as the typical fare?
Show answer
The mean is much higher than the median, so the distribution is right skewed: a few very large fares raise the mean. The median (about 14) is the better description of a typical fare.
Exercise
Write code that prints the count, mean, and median of Age only. Why is the count lower than the number of rows?
Show answer
Use print(df["Age"].agg(["count", "mean", "median"])). The count is lower because some passengers have a missing Age, and these statistics skip missing values.