Kozer H
Kozer H

Reputation: 37

Pandas Dataframe how to get multiple rows where a column is equal to a specific value

I am trying to create a dataframe that contains the entire row value from another dataframe if a specific column is equal to a value. The issue is that I need to do this for 2 different columns. So far my attempts sort of work, but the result is that the second call to the dataframe overwrites the first.

    subsetDataFrame = df[df['Base Price Check'] == 'False']
    subsetDataFrame = df[df['MSRP Price Check'] == 'False']

All I want is to either combine this into one line, or somehow make 2 work. This will not work as I expect if the MSRP Price Check column is something other than false. It overwrites the first column. I feel this should be an easy fix but I have not found it yet.

Upvotes: 0

Views: 1004

Answers (1)

Danyal Imran
Danyal Imran

Reputation: 2605

Combine them via logical operators: and &, or |, not ~.

subsetDataFrame = df[(df['Base Price Check'] == 'False') & (df['MSRP Price Check'] == 'False')]

The above code is going to filter rows where both Base Price Check and MSRP Price Check is False (you can alter your logic accordingly).

Upvotes: 4

Related Questions