[Python] Compare two columns using pandas

Discussão em 'Python' iniciado por Stack, Agosto 20, 2025.

  1. Stack

    Stack Membro Participativo

    Using this as a starting point:

    a = [['10', '1.2', '4.2'], ['15', '70', '0.03'], ['8', '5', '0']]
    df = pd.DataFrame(a, columns=['one', 'two', 'three'])


    which looks like

    one two three
    0 10 1.2 4.2
    1 15 70 0.03
    2 8 5 0


    I want to use something like an if statement within pandas.

    if df['one'] >= df['two'] and df['one'] <= df['three']:
    df['que'] = df['one']


    Basically, create a new column by checking each row via the if statement.

    The docs say to use .all but there is no example...

    Continue reading...

Compartilhe esta Página