Selecting columns with condition on Pandas DataFrame
You can use all
with boolean indexing
:
print ((df == 'something1').all(1))0 True1 False2 True3 False4 Falsedtype: boolprint (df[(df == 'something1').all(1)]) col1 col20 something1 something12 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 col30 something1 something1 a1 something2 something3 s2 something1 something1 r3 something2 something3 a4 something1 something2 acols = df.columns[df.columns.isin(['col1','col2'])]print (cols)Index(['col1', 'col2'], dtype='object')print (df[(df[cols] == 'something1').all(1)]) col1 col2 col30 something1 something1 a2 something1 something1 r
Why not:
df[(df.col1 == 'something1') | (df.col2 == 'something1')]
outputs:
col1 col20 something1 something12 something1 something14 something1 something2
To apply one condition to the whole dataframe
df[(df == 'something1').any(axis=1)]