pythonpandasdataframe

Dataframe fill for null values - Pandas


I encounter problems trying to fill all null values on a specific column of a data frame.

Here is an example of dataframe and my expected outcome.

Example data frame:

Column_1 Column_2
    F      A
   None    B
   None   None
    G      C
   None   None
   None    D 
    H      D

I want to get the first value from the column 1 to all null value from column 2

Expected Outcome:

Column_1 Column_2
    F      A
   None    B
   None    G #First value from the left column
    G      C
   None    H #First value from the left column
   None    D 
    H      D

I'm getting error when I try this code.

df['Colunmn_2'].ffill(df.loc[df['Column_1'].first_valid_index(), 'Column_1'],inplace=True)

Thanks in advance!


Solution

  • You can combine fillna on Column_2 and bfill on Column_1:

    df['Column_2'] = df['Column_2'].fillna(df['Column_1'].bfill())
    

    Output:

      Column_1 Column_2
    0        F        A
    1      NaN        B
    2      NaN        G
    3        G        C
    4      NaN        H
    5      NaN        D
    6        H        D
    

    Intermediates:

      Column_1 Column_2 col1_bfill col2_fillna
    0        F        A          F           A
    1      NaN        B          G           B
    2      NaN      NaN          G  ------>  G
    3        G        C          G           C
    4      NaN      NaN          H  ------>  H
    5      NaN        D          H           D
    6        H        D          H           D