Test if any column of a pandas DataFrame satisfies a condition
ne
is the method form of !=
. I use that so that pipelining any
looks nicer. I use any(axis=1)
to find if any are true in a row.
df['indicator'] = df[columns].ne(0).any(axis=1)
In this particular case you could also check whether the sum of corresponding columns !=0
:
df['indicator'] = df[columns].prod(axis=1).ne(0)
PS @piRSquared's solution is much more generic...
Maybe using min
df['indicator']=(df[columns]!=0).min(axis=1).astype(bool)