Lesson 05
Series Statistics
Summarize a Series with sum, mean, median, std, min and max, describe, value_counts, unique, nunique, nlargest, and cumulative totals.
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.
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.
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:
total: 280.0
size: 6 count: 5 missing: 1
mean: 56.0 median: 45.0
min: 30.0 max: 100.0 std: 26.79sum 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.
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:
cy ana
cy 100
eli 60
dtype: int64
ana 30
bex 45
dtype: int64describe
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.
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:
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: objectCounting 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.
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:
['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: float64Running 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.
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:
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: float64Key takeaways
-
Summaries skip missing values, and
meandivides bycount -
idxmaxandnlargestkeep the labels -
value_countscounts each distinct value, largest first -
describechanges shape for text columns -
Next: Creating a DataFrame
-
Previous: Getting and Setting Series Values
Loading playground…
Check your understanding
Series Statistics Quiz
What does s.mean() do with a missing value?
1 / 10