Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 05

Series Statistics

Summarize a Series with sum, mean, median, std, min and max, describe, value_counts, unique, nunique, nlargest, and cumulative totals.

Intermediate25 min
Series Statistics hero Module 24 Lesson 5Series Statistics hero Module 24 Lesson 5

Once values carry labels, a summary can point back at a label. These methods answer how much, how many, how spread out, and which ones. They all skip missing values, which is the main difference from plain Python.

A labelled Series with its two largest values highlightedA labelled Series with its two largest values highlighted

Totals and averages

sum, mean, median, std, min, and max each reduce the Series to one number. size counts every label, while count counts only the values that are present, so the difference is the number of missing values.

python
import numpy as np
import pandas as pd

runs = pd.Series({"ana": 30, "bex": 45, "cy": 100, "dev": 45, "eli": 60, "fin": np.nan})
print("total:", runs.sum())
print("size:", runs.size, "count:", runs.count(), "missing:", runs.isna().sum())
print("mean:", round(runs.mean(), 2), "median:", runs.median())
print("min:", runs.min(), "max:", runs.max(), "std:", round(runs.std(), 2))

Output:

text
total: 280.0
size: 6 count: 5 missing: 1
mean: 56.0 median: 45.0
min: 30.0 max: 100.0 std: 26.79

sum and mean ignore the missing value, and mean divides by count, not by size.

Which label, not just which value

idxmax and idxmin give the label of the largest or smallest value. nlargest and nsmallest keep the labels too, and return them in order.

python
import pandas as pd

runs = pd.Series({"ana": 30, "bex": 45, "cy": 100, "dev": 45, "eli": 60})
print(runs.idxmax(), runs.idxmin())
print(runs.nlargest(2))
print(runs.nsmallest(2))

Output:

text
cy ana
cy     100
eli     60
dtype: int64
ana    30
bex    45
dtype: int64

describe

describe gives the whole numeric summary at once: the count, the mean, the spread, and the quartiles. On text it switches to the count, the number of distinct values, the most common one, and its frequency.

python
import pandas as pd

runs = pd.Series([2, 4, 3, 1, 5, 2, 3, 2, 5, 1, 2])
print(runs.describe())

tracks = pd.Series(["python", "pandas", "sql", "python", "numpy", "python"])
print(tracks.describe())

Output:

text
count    11.000000
mean      2.727273
std       1.420627
min       1.000000
25%       2.000000
50%       2.000000
75%       3.500000
max       5.000000
dtype: float64
count          6
unique         4
top       python
freq           3
dtype: object

Counting distinct values

unique lists the distinct values in the order they first appear. nunique counts them. value_counts is the one you reach for most: how many times each value appears, largest first.

python
import pandas as pd

tracks = pd.Series(["python", "pandas", "sql", "python", "numpy", "python", "pandas"])
print(tracks.unique())
print(tracks.nunique())
print(tracks.value_counts())
print(tracks.value_counts(normalize=True).round(2))

Output:

text
['python' 'pandas' 'sql' 'numpy']
4
python    3
pandas    2
sql       1
numpy     1
Name: count, dtype: int64
python    0.43
pandas    0.29
sql       0.14
numpy     0.14
Name: proportion, dtype: float64

Running totals and products

cumsum and cumprod keep a running result, so the last value is the overall total. prod multiplies everything into one number. agg runs several summaries in one call.

python
import pandas as pd

weekly = pd.Series([120, 85, 140, 60], index=["Mon", "Tue", "Wed", "Thu"])
print(weekly.cumsum())
print(pd.Series([2, 4, 3, 5]).cumprod().tolist())
print(pd.Series([2, 4, 3, 5]).prod())
print(weekly.agg(["min", "max", "mean"]))

Output:

text
Mon    120
Tue    205
Wed    345
Thu    405
dtype: int64
[2, 8, 24, 120]
120
min      60.00
max     140.00
mean    101.25
dtype: float64

Key takeaways

Loading playground…

Check your understanding

Series Statistics Quiz

What does s.mean() do with a missing value?

1 / 10