Search code examples
pandasif-statementcontains

Pandas is condition on multiple columns


I have a dataframe

col1    col2    col3    col4
A         F      F      F
B         F      A      B
C         B      A      C
D         S      A      F

I want to say if A and F in any of these columns then make a new column and enter "Found"

col1    col2    col3    col4   output
A         F      F      F        Found
B         F      A      B        Found
C         B      A      C           0
D         S      A      F        Found

Solution

  • Try this:

    df.loc[df.apply(lambda x: ((x=='F').any() & (x=='A').any()).any(),axis=1), 'output'] = 'Found'

    df.fillna(0)