Descriptive Statistics and Exploratory Analysis
Summarize data with mean, median, spread and percentiles, detect outliers with z-scores and the IQR rule, and measure relationships with correlation.
Welcome back!
Step 1 of 13Learn
Center: mean vs. median
Exploratory data analysis (EDA) means getting to know a dataset with summaries and charts before drawing conclusions. It starts with two questions about each numeric column: where is the center? and how spread out is it?
The two main measures of center behave very differently:
- The mean is the sum divided by the count. It uses every value, so it's pulled toward extreme values.
- The median is the middle value when sorted (the average of the two middle values for an even count). It ignores how extreme the extremes are.
Take five salaries at a small company: $42k, $45k, $48k, $51k and $300k (the founder).
- Mean = (42 + 45 + 48 + 51 + 300) ÷ 5 = $97.2k
- Median = $48k
The mean describes nobody. Four of five people earn less than half of it. For skewed data such as incomes, house prices, website session lengths or delivery times, the median usually tells the more honest story, and reporting both is often wise.
In pandas: df["salary"].mean(), df["salary"].median(), and df["city"].mode() for the most common value of a category. When the mean is much larger than the median, the distribution has a long right tail: a few very large values.
Step 2 of 13Practice · Work it out
Delivery times in days are 2, 3, 3, 4, 5, 6 and 26. What is the median?
Step 3 of 13Learn
Spread: standard deviation and percentiles
Two datasets can share a mean and still be completely different. Daily temperatures of 20, 20, 20 and 0, 20, 40 both average 20.
Standard deviation measures the typical distance of values from the mean. Roughly: square each value's distance from the mean, average those squares, take the square root. For data in a bell-shaped (normal) distribution, about 68% of values fall within 1 standard deviation of the mean and about 95% within 2.
Remember the pandas/NumPy difference: df["x"].std() uses n − 1 in the denominator (sample standard deviation), and np.std(x) uses n unless you pass ddof=1.
Percentiles describe spread without assuming any shape. The 90th percentile is the value below which 90% of observations fall. The quartiles split the data into four parts:
- Q1 = 25th percentile
- Q2 = 50th percentile = median
- Q3 = 75th percentile
The interquartile range (IQR) = Q3 − Q1 is the spread of the middle half of the data, and like the median it ignores extremes.
df.describe() reports count, mean, std, min, 25%, 50%, 75% and max in one call. df["x"].quantile([0.1, 0.9]) gets any percentiles you want. Teams that care about user experience often track the 95th or 99th percentile of response times rather than the average, because the slowest experiences are the ones users remember.
Step 4 of 13Explore
The normal distribution
Adjust the mean and standard deviation to see how the bell curve moves and widens. The readouts show the range that holds about 68% and 95% of values.
- 68% range from
- 70
- 68% range to
- 90
- 95% range from
- 60
- 95% range to
- 100
Step 5 of 13Practice · Work it out
Exam scores are roughly normal with mean 72 and standard deviation 8. About what percentage of students score between 56 and 88?
Step 6 of 13Learn
Finding outliers
Outliers are values far from the rest. They might be errors (a typo turning 45 into 450), rare but real events (a viral sales day), or the most interesting thing in the dataset (fraud). Two common detection rules:
Z-scores. A z-score says how many standard deviations a value lies from the mean: z = (x − mean) / std. Values with |z| greater than 3 are often flagged. The weakness: the outliers themselves inflate the mean and standard deviation, which can hide them in small datasets.
The IQR rule. Flag values below Q1 − 1.5 × IQR or above Q3 + 1.5 × IQR. These are the "whiskers" of a box plot. Because quartiles ignore extremes, this rule is more robust for skewed data.
q1, q3 = df["amount"].quantile([0.25, 0.75])
iqr = q3 - q1
outliers = df[(df["amount"] < q1 - 1.5 * iqr) | (df["amount"] > q3 + 1.5 * iqr)]Detection is the easy part. Then decide: fix it if it's a verifiable error, keep it if it's real (perhaps analyzing with and without it), or use methods that are robust to it, such as medians. Deleting outliers just because they're inconvenient is how analyses end up describing a world that doesn't exist.
Step 7 of 13Try the calculator
Z-score and IQR fences
Enter a value with the column's mean and standard deviation to get its z-score, and the quartiles to get the IQR outlier fences.
- z-score
- 2.5
- Lower IQR fence
- 38
- Upper IQR fence
- 102
Step 8 of 13Practice · Code challenge · Python
Write iqr_outliers(values, q1, q3) that returns the list of values outside the IQR fences: below q1 − 1.5 × (q3 − q1) or above q3 + 1.5 × (q3 − q1). Keep the original order. Values exactly on a fence are not outliers.
iqr_outliers([50, 62, 70, 78, 103, 30, 101], 62, 78)iqr_outliers([1, 2, 3], 1, 3)iqr_outliers([10, 40, -6, 0], 10, 20)
Step 9 of 13Learn
Relationships: correlation
EDA also asks how variables move together. The Pearson correlation coefficient r measures the strength of a linear relationship between two numeric variables, from −1 to +1:
- r near +1: as one rises, the other rises along a fairly straight line (temperature and ice-cream sales).
- r near −1: as one rises, the other falls (price and quantity demanded).
- r near 0: no linear relationship.
df["temp"].corr(df["rentals"]) # one pair
df[["temp", "rain_mm", "rentals"]].corr() # a correlation matrixThree cautions every analyst learns:
- Correlation is not causation. Ice-cream sales and drownings are correlated because both rise in hot weather, a confounding variable. Showing cause needs experiments or careful causal methods.
- r only measures straight-line patterns. A perfect U-shaped relationship can have r ≈ 0. Always look at a scatter plot.
- Outliers can create or destroy correlation. A single extreme point can move r a lot. Spearman's rank correlation,
corr(method="spearman"), is more robust and catches any consistently increasing or decreasing pattern.
Step 10 of 13Practice · Match the pairs
Match each correlation value to the best description.
Tap an answer, then tap the place where it belongs (or drag it there).
- r = 0.92
- r = −0.85
- r = 0.05
- r = 0.45
Step 11 of 13Learn
Comparing groups fairly
A frequent EDA question is whether two groups differ: members versus casual riders, this month versus last month, store A versus store B. A few habits keep comparisons honest.
Compare like with like. Total rentals per station mostly reflect station size. Rentals per dock, or the share of capacity used, compare stations fairly. Rates, averages and percentages usually beat raw totals.
Look at distributions, not just averages. Two groups with the same mean can differ a lot in spread or shape. Side-by-side box plots or histograms show it at a glance.
Mind the group sizes. A station with 12 rentals and an average trip of 41 minutes isn't comparable in reliability to one with 12,000 rentals. Small groups produce noisy, extreme averages; report counts next to every average.
Watch for mixed groups. An overall trend can reverse inside every subgroup, a pattern known as Simpson's paradox. If members ride longer overall but casual riders ride longer at every single station, the difference comes from where each group rides, not from membership itself. Breaking results down by an important third variable is the defense.
Formal hypothesis tests can quantify whether a difference is larger than chance would produce, but these habits come first.
Step 12 of 13Practice · What would you do?
What would you do?
Store A reports 4,000 sales this month and store B reports 2,500. Store A is four times larger by floor space. A manager concludes that store A is performing better. What's the best response?
Step 13 of 13Practice · True or false
A correlation of 0.9 between two variables shows that one causes the other.
Lesson complete!