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!
a | b | c |
---|---|---|
True | False | True |
False | True | False |
False | False | False |
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
Use DataFrame.any
:
df['d'] = df.any(axis=1)
If need specify columns for test:
df['d'] = df[['a','b','c']].any(axis=1)