[Python] Filtering Pandas Dataframe for even or odd int values

Discussão em 'Python' iniciado por Stack, Março 3, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to filter a data frame if the column contains even or odd values. For example I would like this table:

    Player​
    Score
    A​
    15
    B​
    14
    C​
    12​

    To look like this when filtering for Score even values:

    Player​
    Score
    B​
    14
    C​
    12​

    My working solution is to create a new column using the modulo operator (%) to divide the values by 2 and then filter if it is a 1 or 0.

    df['even_odd'] = df.Score % 2


    Is there a better way?

    Continue reading...

Compartilhe esta Página