Home >Backend Development >Python Tutorial >How to Fill NaN Values in a Pandas DataFrame with Preceding or Following Values?
Consider a DataFrame with NaNs:
In [1]: import pandas as pd df = pd.DataFrame([[1, 2, 3], [4, None, None], [None, None, 9]]) In [2]: df 0 1 2 0 1 2 3 1 4 NaN NaN 2 NaN NaN 9
The goal is to replace each NaN with the closest non-NaN value in the same column above it. Assume the first row never contains NaNs.
An efficient way to achieve this is to use the fillna method of the DataFrame:
In [3]: df.fillna(method='ffill') 0 1 2 0 1 2 3 1 4 2 3 2 4 2 9
The fillna method employs the forward fill (ffill) strategy, which replaces NaNs with the last valid observation in that column.
To do the opposite, you can use the bfill method (backward fill):
In [4]: df.fillna(method='bfill') 0 1 2 0 1 2 3 1 4 4 3 2 9 9 9
The fillna method doesn't modify the DataFrame inplace. To update the original DataFrame, set inplace=True:
In [5]: df.fillna(method='ffill', inplace=True) In [6]: df 0 1 2 0 1 2 3 1 4 2 3 2 4 2 9
The above is the detailed content of How to Fill NaN Values in a Pandas DataFrame with Preceding or Following Values?. For more information, please follow other related articles on the PHP Chinese website!