Missing Values
Fill with Column Mean
Replace missing values with the mean of the observed (non-missing) values in
the same column. By hand, compute the mean in a first pass (sum and count of
non-None values), then fill in a second pass. With pandas, Series.fillna (Series.mean()) combines both steps — mean() skips NaN by default.
By hand
With pandas
s.mean() returns the mean of all non-NaN entries (skipna is True by
default). Passing it straight to fillna substitutes that value at every NaN
position. The snapshot shows the original series under before: and the
computed mean so both substitution points are visible.
naive.py
values = [3.1, None, 7.2, None, 5.0, None, 8.4, 2.9]
total = 0.0
count = 0
for v in values:
if v is not None:
total = total + v
count = count + 1
mean = round(total / count, 2)
filled = []
for v in values:
filled.append(v if v is not None else mean)
print('RESULT:', [round(v, 2) for v in filled])
library.py
import pandas as pd
from dalib.display import set_display
set_display()
values = [3.1, None, 7.2, None, 5.0, None, 8.4, 2.9]
s = pd.Series(values, dtype=float)
m = round(s.mean(), 2)
filled = s.fillna(m)
result = [round(v, 2) for v in filled.tolist()]
print('index:', s.index.tolist())
print('dtype:', s.dtype)
print('before:', s.tolist())
print('mean:', m)
print('RESULT:', result)
index: [0, 1, 2, 3, 4, 5, 6, 7]
dtype: float64
before: [3.1, nan, 7.2, nan, 5.0, nan, 8.4, 2.9]
mean: 5.32
RESULT: [3.1, 5.32, 7.2, 5.32, 5.0, 5.32, 8.4, 2.9]
Implementation notes
Series.mean()hasskipna=Trueby default — it ignoresNaNwhen computing the average, which is exactly the behaviour the naive loop replicates with theif v is not Noneguard.- Mean imputation preserves the column mean but reduces variance and can distort correlations. It is most appropriate when data is missing at random and the proportion of missing values is small.
- For a median or mode fill instead of mean, use
s.fillna(s.median())ors.fillna(s.mode()[0]). - Cross-reference:
fill-constant(this chapter) for the simpler case of filling with a fixed value rather than a computed statistic. - Cross-reference:
list-sum-mean(python-data-basics) for the underlying sum-and-divide mean computation this naive half applies.