R
Rishtaara
Python: Zero to Professional
Lesson 30 of 40Article16 min

Statistics — Mean, Median, Mode & More

Statistics summarize data — central tendency, spread, and distribution shape.

Statistics with Python

Statistics summarize data — central tendency, spread, and distribution shape.

Pandas describe() shows count, mean, std, min, quartiles, max in one table.

Real-life example: Statistics are a weather report summary — not every hourly reading, but today's high, low, and average.

describe()
import pandas as pd

df = pd.DataFrame({"score": [80, 85, 90, 92, 88]})
print(df.describe())

Mean, Median, Mode

Mean is average; median is middle value; mode is most frequent.

Use median when outliers skew the mean (e.g. one very high salary).

Real-life example: Mean house price in a street with one mansion misleads — median tells a fairer story.

Central tendency
import statistics

data = [2, 3, 3, 5, 7]
print(statistics.mean(data))
print(statistics.median(data))
print(statistics.mode(data))

Standard Deviation

Standard deviation measures spread around the mean. Low std = values clustered; high std = scattered.

Many ML models assume features are on similar scales — std helps you check that.

Real-life example: Std dev is how spread out arrival times are — buses on time vs randomly late.

Spread
import statistics

tight = [10, 11, 10, 9, 10]
wide = [1, 15, 8, 20, 3]
print(statistics.stdev(tight), statistics.stdev(wide))

Percentile

The 25th percentile (Q1) means 25% of values fall below it. Median is the 50th percentile.

Percentiles rank students, response times, and income bands.

Real-life example: Top 10% exam score means your mark is above the 90th percentile.

Percentile with NumPy
import numpy as np

scores = [55, 60, 72, 78, 90, 95]
print(np.percentile(scores, 25))
print(np.percentile(scores, 90))

Class Distribution

For classification, check how many examples per class. Imbalanced sets need special care.

value_counts() or groupby().size() in Pandas reveals balance.

Real-life example: Class distribution is counting yes vs no votes — a 99–1 split needs different handling than 50–50.

Count classes
import pandas as pd

df = pd.DataFrame({"label": ["spam", "ham", "ham", "ham", "spam"]})
print(df["label"].value_counts())