[Python] How to add multiple columns to pandas dataframe in one assignment

Discussão em 'Python' iniciado por Stack, Junho 18, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to figure out how to add multiple columns to pandas simultaneously with Pandas. I would like to do this in one step rather than multiple repeated steps.

    import pandas as pd

    data = {'col_1': [0, 1, 2, 3],
    'col_2': [4, 5, 6, 7]}
    df = pd.DataFrame(data)


    I thought this would work here...

    df[['column_new_1', 'column_new_2', 'column_new_3']] = [np.nan, 'dogs', 3]

    Continue reading...

Compartilhe esta Página