[Python] Dataframe fill for null values - Pandas

Discussão em 'Python' iniciado por Stack, Março 13, 2025.

  1. Stack

    Stack Membro Participativo

    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!

    Continue reading...

Compartilhe esta Página