[Python] How do you filter pandas dataframes by multiple columns?

Discussão em 'Python' iniciado por Stack, Maio 15, 2025.

  1. Stack

    Stack Membro Participativo

    To filter a DataFrame (df) by a single column, if we consider data with male and females we might:

    males = df[df[Gender]=='Male']


    Question 1: But what if the data spanned multiple years and I wanted to only see males for 2014?

    In other languages I might do something like:

    if A = "Male" and if B = "2014" then


    (except I want to do this and get a subset of the original DataFrame in a new dataframe object)

    Question 2: How do I do this in a loop, and create a dataframe object for each unique sets of year and gender (i.e. a df for: 2013-Male, 2013-Female, 2014-Male, and 2014-Female?

    for y in year:

    for g in gender:

    df = .....

    Continue reading...

Compartilhe esta Página