fillna() (Pandas)
fillna() replaces missing entries with specified values, such as a constant or a per-column mapping. Use the separate ffill() and bfill() methods to carry values through an ordered sequence.
Pandas
A scalar fill such as fillna(0) replaces missing entries where the dtype permits that value. On a DataFrame, a mapping can provide different replacements for selected columns. Unmentioned columns retain their missing values; choose each replacement according to what the data means.
ffill() carries the preceding known value and bfill() uses a following one in the current row order. The older fillna(method="ffill") form was deprecated in Pandas 2.1 and removed in Pandas 3.0. Establish the correct order and entity boundaries before carrying values.
Filling changes what a report knows about its source. Keep an isna() flag before replacing values when an imputed value must remain distinguishable from a measured one. Leaving a value unknown or dropping a record can be appropriate under a different explicit policy.
Syntax
df.fillna(value) | df.ffill() | df.bfill()Examples
Python
import pandas as pd
readings = pd.Series([None, 10.0, None, None, 20.0], dtype="float64")
originally_missing = readings.isna()
filled = readings.ffill(limit=1)
print(originally_missing.tolist())
print(filled.isna().tolist())
print(filled.dropna().tolist())Output:
[True, False, True, True, False]
[True, False, False, True, False]
[10.0, 10.0, 20.0]