Statistics
Python · Reference cheat sheet
Statistics
Python · Reference cheat sheet
📋 Overview
statistics is the stdlib module for basic descriptive stats on numeric data. For heavy analysis and DataFrames, prefer pandas / NumPy.
🔧 Core concepts
| API | Role |
|---|---|
mean / fmean | Arithmetic mean (fmean faster for floats) |
median / mode | Middle value / most common |
pstdev / stdev | Population / sample stdev |
pvariance / variance | Variance |
quantiles | Cut points |
correlation | Pearson correlation (3.10+) |
💡 Examples
from statistics import mean, median, mode, stdev
xs = [2, 4, 4, 4, 5, 5, 7, 9]
print(mean(xs), median(xs), mode(xs))
print(stdev(xs))from statistics import quantiles, correlation
data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(quantiles(data, n=4)) # quartiles
print(correlation([1, 2, 3], [2, 4, 6]))⚠️ Pitfalls
- Empty sequences raise
StatisticsError. moderaises if no unique mode (usemultimode).- Sample vs population (
stdevvspstdev) — know which you need. - Not a substitute for pandas groupby / NumPy vectorized ops on large data.