[Python] Pandas - fillna multiple columns with a given series, matching by index?

Discussão em 'Python' iniciado por Stack, Abril 21, 2025.

  1. Stack

    Stack Membro Participativo

    I'd like to use a fillna command to fillna multiple columns of a Pandas dataframe with the same series, matching by index:

    import numpy as np
    import pandas as pd

    df_1 = pd.DataFrame(index = [0, 1, 2], columns = ['a', 'b', 'c'], data = [[1, np.NaN, np.NaN], [2, np.NaN, 4], [np.NaN, 2, -3]])
    series_1 = pd.Series([6, 5, 4], index = [0, 1, 2])

    df_2 = df_1.fillna(series_2) # ???


    Desired output is

    df_2 = pd.DataFrame(index = [0, 1, 2], columns = ['a', 'b', 'c'], data = [[1, 6, 6], [2, 5, 4], [4, 2, -3]])


    Is there a pythonic way to do this without individually fillnaing every column?

    Continue reading...

Compartilhe esta Página