[Python] Update a dataframe by dataframes with NaN values

Discussão em 'Python' iniciado por Stack, Setembro 5, 2025.

  1. Stack

    Stack Membro Participativo

    I try to update a DataFrame

    df1 = pd.DataFrame(data = {'A' : [1,2,3,4], 'B' : [5,6,7,8]})


    by another DataFrame

    df2 = pd.DataFrame(data = {'B' : [9, np.nan, 11, np.nan]}).


    Now, my aim is to update df1 by df2 and overwrite all values (NaN values too) using

    df1.update(df2)


    In contrast with the common usage it's important to me to get the NaN values finally in df1. But as far as I see the update returns

    >>> df1
    A B
    0 1 9
    1 2 6
    2 3 11
    3 4 8


    Is there a way to get

    >>> df1
    A B
    0 1 9
    1 2 NaN
    2 3 11
    3 4 NaN


    without building df1 manually?

    Continue reading...

Compartilhe esta Página