index a Python Pandas dataframe with multiple conditions SQL like where statement

后端 未结 1 1771
遇见更好的自我
遇见更好的自我 2020-12-09 21:34

I am experienced in R and new to Python Pandas. I am trying to index a DataFrame to retrieve rows that meet a set of several logical conditions - much like the \"where\" st

相关标签:
1条回答
  • 2020-12-09 21:57
    # subset1:  All rows and columns where:
    #   (fruit in fruitsInclude) AND (Vegetable not in vegetablesExlude)
    df.ix[df['Fruit'].isin(fruitsInclude) & ~df['Vegetable'].isin(vegetablesExclude)]
    
    # subset2:  All rows and columns where:
    #   (fruit in fruitsInclude) AND [(Vegetable not in vegetablesExlude) OR (Animal == 'Dog')]
    df.ix[df['Fruit'].isin(fruitsInclude) & (~df['Vegetable'].isin(vegetablesExclude) | (df['Animal']=='Dog'))]
    
    # subset3:  All rows and specific columns where above logical conditions are true.
    df.ix[df['Fruit'].isin(fruitsInclude) & ~df['Vegetable'].isin(vegetablesExclude) & (df['Animal']=='Dog')]
    
    0 讨论(0)
提交回复
热议问题