[Python] How to check if a column exists in Pandas

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

  1. Stack

    Stack Membro Participativo

    How do I check if a column exists in a Pandas DataFrame df?

    A B C
    0 3 40 100
    1 6 30 200


    How would I check if the column "A" exists in the above DataFrame so that I can compute:

    df['sum'] = df['A'] + df['C']


    And if "A" doesn't exist:

    df['sum'] = df['B'] + df['C']

    Continue reading...

Compartilhe esta Página