Replacing missing values in Pandas DataFrame
Start your free 7-days trial now!
To replace missing values (NaN
) in Pandas DataFrame, use the fillna(~)
method.
Replacing missing value with a specific value
Consider the following DataFrame:
df
A B0 3.0 NaN1 NaN 6.0
To fill all missing values with the value 8
:
A B0 3.0 8.01 8.0 6.0
Replacing missing values in certain columns
Consider the same df
as above:
df
A B0 3.0 NaN1 NaN 6.0
To fill missing values in certain columns:
A B0 3.0 NaN1 8.0 6.0
Here, we are replacing all missing values in column A
with 8.
Replacing missing values with value in preceding row
Consider the following DataFrame:
df
A B0 3.0 NaN1 NaN 6.02 5.0 7.0
To fill missing values with value in the preceding row:
A B C0 3.0 NaN 8.01 3.0 6.0 8.02 5.0 7.0 8.0
Here, note the following:
method="ffill"
standard for forward-fill, that is, we replace a missing value with the previous non-NaN
value in the same column.we still have
NaN
in columnB
because there is no previous row.
Replacing missing values with value in next row
Consider the following DataFrame:
df
A B C0 3.0 6.0 NaN1 NaN 7.0 NaN2 5.0 NaN 8.0
To fill missing values with value in the next row:
A B C0 3.0 6.0 8.01 5.0 7.0 8.02 5.0 NaN 8.0
Here, note the following:
method="bfill"
standard for backward-fill, that is, we replace a missing value with the next non-NaN
value in the same column.we still have
NaN
in columnB
because there is no next row.