Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting columns with condition on Pandas DataFrame

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?

like image 827
user3368526 Avatar asked Jun 06 '16 17:06

user3368526


3 Answers

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
like image 199
jezrael Avatar answered Nov 15 '22 01:11

jezrael


Why not:

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

outputs:

    col1    col2
0   something1  something1
2   something1  something1
4   something1  something2
like image 43
Naomi Fridman Avatar answered Nov 14 '22 23:11

Naomi Fridman


To apply one condition to the whole dataframe

df[(df == 'something1').any(axis=1)]
like image 1
Sincole Brans Avatar answered Nov 15 '22 01:11

Sincole Brans