Location Research Breakthrough Possible @S-Logix pro@slogix.in

How to Replace NaN Values by Forward and Backward Method in Python

Forward and Backward Fill in Dataset

Condition for Replacing NaN by Forward and Backward Method in a Dataset

  • Description:
    Forward and backward filling are methods to replace NaN values in a dataset using the values from adjacent non-missing data.

    Forward Fill (ffill): Replaces missing values with the most recent preceding non-missing value in the same column.

    Backward Fill (bfill): Replaces missing values with the next available non-missing value in the same column.
Step-by-Step Process
  • Import Libraries:
    Import necessary libraries like Pandas.
  • Load or Create Data:
    Prepare your dataset with missing values.
  • Identify Missing Values:
    Use isna() to locate NaN values.
  • Apply Forward Fill (ffill):
    Use the fillna(method='ffill') to replace NaN with the previous valid value.
  • Apply Backward Fill (bfill):
    Use the fillna(method='bfill') to replace NaN with the next valid value.
  • Combine Both Methods:
    Use a combination of forward and backward filling to ensure no NaN values remain.
Sample Source Code
  • # Code for Replacing NaN Values by Forward and Backward Method

    import pandas as pd

    data = {
    'Date': ['2024-12-01', '2024-12-02', '2024-12-03', '2024-12-04', '2024-12-05'],
    'Temperature': [22.5, None, 23.0, None, 24.5],
    'Humidity': [55, 60, None, None, 70]
    }

    df = pd.DataFrame(data)

    print("Original DataFrame:")
    print(df)

    # Apply forward fill
    df_ffill = df.fillna(method='ffill')

    # Display the DataFrame after forward filling
    print("\nDataFrame after Forward Fill:")
    print(df_ffill)

    # Apply backward fill
    df_bfill = df.fillna(method='bfill')

    # Display the DataFrame after backward filling
    print("\nDataFrame after Backward Fill:")
    print(df_bfill)

    # Apply both (forward followed by backward fill)
    df_combined = df.fillna(method='ffill').fillna(method='bfill')

    # Display the DataFrame after combining forward and backward fill
    print("\nDataFrame after Combining Forward and Backward Fill:")
    print(df_combined)
Screenshots
  • Forward and Backward Fill Output