Sub topic 5
Turning counts into proportions
This chapter assumes you can build a DataFrame, select a column, and call value_counts on it. A count of 3 means little until you know the total. A proportion (a fraction of the total) answers that.
The normalize argument
Pass normalize=True to value_counts to get fractions instead of counts. The fractions add up to 1.
import pandas as pd
df = pd.DataFrame({
"city": ["Oslo", "Rome", "Oslo", "Paris", "Rome", "Oslo", None],
"plan": ["free", "pro", "free", "pro", "pro", "pro", "free"],
})
shares = df["city"].value_counts(normalize=True)
print(shares)
# Oslo 0.500000
# Rome 0.333333
# Paris 0.166667The column has 7 rows, but one city is missing. By default value_counts drops missing values before it counts. The denominator is therefore 6, the number of non-missing values, and not 7.
Missing values change the denominator
Set dropna=False to keep the missing value as its own entry. The denominator then becomes 7.
shares_all = df["city"].value_counts(normalize=True, dropna=False)
# Oslo 0.428571 (3 / 7)
# Rome 0.285714 (2 / 7)
# Paris 0.142857 (1 / 7)
# NaN 0.142857 (1 / 7)The two calls give different answers for the same city. Oslo is 50% of the known cities and about 43% of all rows. Decide which question you are asking before you report a share.
| Call | Denominator | Oslo share |
|---|---|---|
| `value_counts(normalize=True)` | 6 (non-missing only) | 0.50 |
| `value_counts(normalize=True, dropna=False)` | 7 (all rows) | 0.43 |
Percentages and manual normalization
normalize=True returns fractions. Multiply by 100 for percentages, and round for display.
percent = (df["city"].value_counts(normalize=True) * 100).round(1)
# Oslo 50.0
# Rome 33.3
# Paris 16.7You get the same result by dividing the counts by their sum:
counts = df["city"].value_counts()
manual = counts / counts.sum() # identical to normalize=TrueThe manual form is useful when you want both columns in one table.
summary = pd.DataFrame({
"count": counts,
"share": counts / counts.sum(),
})A shortcut for one category
To find the share of rows that match one condition, take the mean of a boolean Series. True counts as 1 and False as 0.
pro_share = (df["plan"] == "pro").mean() # 4 / 7, about 0.571Be careful with this on a column that has missing values. A comparison with a missing value gives False, so those rows stay in the denominator.
Common errors
- Reading a share as a share of all rows when the missing values were dropped. Check the denominator.
- Rounding before you sum. Rounded shares may add up to 99.9 or 100.1, so round only for display.
- Using
normalize=Trueon counts you have already computed. It is an argument ofvalue_countsand does not apply to an existing Series.
Exercise
A column has 10 rows. Two values are missing, and the value "A" appears 4 times. What does value_counts(normalize=True)["A"] return, and what does it return with dropna=False?
Show answer
By default the denominator is the 8 non-missing values, so the share of "A" is 4 / 8 = 0.5. With dropna=False the denominator is all 10 rows, so the share is 4 / 10 = 0.4.