Replace missing values in a numeric sequence with a fixed default. By hand, loop over the values and substitute None entries with 0.0. With pandas, Series.fillna(0) replaces every NaN in one call and preserves the float64 dtype throughout.

By hand

With pandas

s.fillna(0) returns a new Series with every NaN replaced by 0. The snapshot shows the original series (with nan) under before: and the filled values in RESULT:, making the substitution positions visible.

naive.py
values = [3.1, None, 7.2, None, 5.0, None, 8.4, 2.9]
filled = []
for v in values:
    filled.append(v if v is not None else 0.0)
print('RESULT:', 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)
filled = s.fillna(0)
result = filled.tolist()
print('index:', s.index.tolist())
print('dtype:', s.dtype)
print('before:', s.tolist())
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]
RESULT: [3.1, 0.0, 7.2, 0.0, 5.0, 0.0, 8.4, 2.9]

Implementation notes

  • fillna(0) preserves float64 — the filled positions become 0.0, not the integer 0. This keeps the dtype uniform and avoids surprises in downstream arithmetic.
  • For non-numeric columns, pass a string default: s.fillna('unknown').
  • To fill with the column mean instead of a constant, see fill-mean (this chapter).
  • Cross-reference: detect-missing (this chapter) to audit which positions were missing before filling.