Replacing NaNs with Preceding or Next Values in Pandas DataFrames
When dealing with Pandas DataFrames, missing data represented as NaNs (Not a Number) can pose a challenge for data analysis. One common task is to replace these NaNs with appropriate values derived from existing data. A straightforward approach is to iterate through the DataFrame and modify values explicitly. However, Pandas offers more efficient solutions that avoid the use of loops.
Forward Filling (ffill)
To replace NaNs with the first non-NaN value above them in the same column, use the fillna method with the ffill (forward fill) option. This method propagates the last valid observation forward to subsequent valid observations.
import pandas as pd df = pd.DataFrame([[1, 2, 3], [4, None, None], [None, None, 9]]) df.fillna(method='ffill')
Output:
0 1 2 0 1 2 3 1 4 2 3 2 4 2 9
Additional Considerations
df.fillna(method='ffill', inplace=True)
The above is the detailed content of How to Efficiently Replace NaN Values in Pandas DataFrames using `ffill` and `bfill`?. For more information, please follow other related articles on the PHP Chinese website!