22

I have a dataframe looking like this.

    col1    col2
0   something1  something1
1   something2  something3
2   something1  something1
3   something2  something3
4   something1  something2  

I'm trying to filter all rows that have something1 either on col1 or col2. If I just need the condition logic on a column, I can do it with df[df.col1 == 'something1'] but would there be a way to do it with multiple columns?

3 Answers 3

16

You can use all with boolean indexing:

print ((df == 'something1').all(1))
0     True
1    False
2     True
3    False
4    False
dtype: bool

print (df[(df == 'something1').all(1)])
         col1        col2
0  something1  something1
2  something1  something1

EDIT:

If need select only some columns you can use isin with boolean indexing for selecting desired columns and then use subset - df[cols]:

print (df)
         col1        col2 col3
0  something1  something1    a
1  something2  something3    s
2  something1  something1    r
3  something2  something3    a
4  something1  something2    a

cols = df.columns[df.columns.isin(['col1','col2'])]
print (cols)
Index(['col1', 'col2'], dtype='object')

print (df[(df[cols] == 'something1').all(1)])
         col1        col2 col3
0  something1  something1    a
2  something1  something1    r
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks jezrael! Additional question: If I want to filter for specific columns (say, only in col1 and col2 but I have other columns), do you know how to do it? I guess I need to replace .all(1) with something else?
hmmm, these columns has common part of column name? - col for col1 and col2 ?
Oh for example, if I have col1, col2 and col3 but I want to look through only col1 and col2 but not col3.
15

Why not:

df[(df.col1 == 'something1') | (df.col2 == 'something1')]

outputs:

    col1    col2
0   something1  something1
2   something1  something1
4   something1  something2

1 Comment

Why not...? :):)
2

To apply one condition to the whole dataframe

df[(df == 'something1').any(axis=1)]

Comments

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.