21

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?

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
user3368526
  • 2,168
  • 10
  • 37
  • 52

3 Answers3

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 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? – user3368526 Jun 06 '16 at 18:01
  • hmmm, these columns has common part of column name? - `col` for `col1` and `col2` ? – jezrael Jun 06 '16 at 18:05
  • 1
    Oh for example, if I have col1, col2 and col3 but I want to look through only col1 and col2 but not col3. – user3368526 Jun 06 '16 at 18:06
14

Why not:

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

outputs:

    col1    col2
0   something1  something1
2   something1  something1
4   something1  something2
Naomi Fridman
  • 2,095
  • 2
  • 25
  • 36
1

To apply one condition to the whole dataframe

df[(df == 'something1').any(axis=1)]
Sincole Brans
  • 186
  • 12