pythonpandasdataframebooleanany

per-row boolean operations in pandas


I have a Pandas Dataframe (see below). I'm trying to find a fast (and concise!) way to generate a column d containing a boolean, that is true, if any of a, b or c in the corresponding row are True. without using a for loop. I just couldn't get it to work using vectorization. Thanks in advance!

my Dataframe:

a b c
True False True
False True False
False False False

what I've tried:

I tried using any(), but that evaluates to True, if any value in the entire frame equals True.

I also managed to achieve the correct result using a for loop and apply any() on every single row, but after a certain number of rows that's just too slow.

edit: fixed table formatting


Solution

  • Use DataFrame.any:

    df['d'] = df.any(axis=1)
    

    If need specify columns for test:

    df['d'] = df[['a','b','c']].any(axis=1)