[Python] Python Pandas: How to filter rows with multiple conditions in a DataFrame

Discussão em 'Python' iniciado por Stack, Setembro 6, 2025.

  1. Stack

    Stack Membro Participativo

    I’m working with a Pandas DataFrame in Python and trying to filter rows based on multiple conditions. I want to select rows where the Age column is greater than 25 and the Name is not "Charlie".

    I’ve seen similar questions about filtering rows with multiple conditions, but they don’t seem to work for my specific case where I have string comparisons along with numeric conditions.

    Here’s my DataFrame:


    import pandas as pd

    data = {'Name': ['Alice','Bob','Charlie'], 'Age':[25,30,35]}
    df = pd.DataFrame(data)


    I first tried filtering with a single condition:


    result = df[df['Age'] > 25]
    print(result)



    This returned rows where Age > 25, but it still included Charlie, which I didn’t want.

    Then I tried combining conditions with & and parentheses:

    result = df[(df['Age'] > 25) & (df['Name'] != 'Charlie')]
    print(result)



    However, I’m not getting the expected output. I expected only Bob’s row to be returned:

    Name Age
    1 Bob 30



    I’m not sure why Charlie is still included and how to correctly filter rows using multiple conditions in Pandas.

    Continue reading...

Compartilhe esta Página