1

I am trying to search for a single criteria throughout multiple dataframe columns using Python. The script I am using is not giving me the output I would hope for. The code is the following:

df = df[df['Account Category' or 'Account Category 2' or 'Account Category 3'] == "Amazon.com and Pantry"]

The criteria I am searching for appears in multiple columns. However, with this code, it is only showing that criteria for the first column. Any help would be appreciated

1 Answer 1

1

Essentially, instead of saying "I want any of these 3 columns to equal something", you generally say "I want col1 to equal something, or col2 to equal something, or col 3 ..."

In pandas, you express that by combining boolean expressions like this, using | for or and & for and:

value = 'Amazon.com and Pantry'
filtered = df.loc[
    (df['Account Category'] == value) 
    | (df['Account Category 2'] == value) 
    | (df['Account Category 3'] == value)]

The pandas documentation is really good, here is a link to the relevant section explaining boolean indexing, which is what you're trying to do!

Sign up to request clarification or add additional context in comments.

2 Comments

Great answer. For complex conditions it's a good idea to create them as variables c1, c2, c3, etc. Then df.loc[c1 | c2 | c3]. Readability! :)
Good point. Saving the mask like that also improves speed if it needs to be reused down the road!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.