Home > Backend Development > Python Tutorial > How to Efficiently Replace NaN Values in Pandas DataFrames using `ffill` and `bfill`?

How to Efficiently Replace NaN Values in Pandas DataFrames using `ffill` and `bfill`?

Patricia Arquette
Release: 2024-12-01 19:05:10
Original
952 people have browsed it

How to Efficiently Replace NaN Values in Pandas DataFrames using `ffill` and `bfill`?

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')
Copy after login

Output:

   0  1  2
0  1  2  3
1  4  2  3
2  4  2  9
Copy after login

Additional Considerations

  • The first row in the DataFrame should not contain NaNs as it serves as the reference point for filling.
  • The ffill method operates column-wise, meaning it fills NaNs in each column separately.
  • To perform backward filling (replacing NaNs with the next non-NaN value below them), use the bfill (backward fill) option.
  • To modify the DataFrame inplace without creating a new variable, set the inplace parameter to True:
df.fillna(method='ffill', inplace=True)
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template