Join 3 boolean columns into 1 taking into account the row value
Question:
Be the following DataFrame in pandas:
Column_1
Column_2
Column_3
Column_4
Column_5
82198
True
False
False
red
27498
False
False
False
red
84838
False
False
True
red
10498
False
True
False
red
I want to create a New_column
that has the value True if any of the columns Column_2
, Column_3
, Column_4
is True and False, if all 3 are false. Output example:
Column_1
Column_2
Column_3
Column_4
Column_5
New_column
82198
True
False
False
red
True
27498
False
False
False
red
False
84838
False
False
True
red
True
10498
False
True
False
red
True
Answers:
this might work:
df['new_column']=(df['Column_2']==True) | (df['Column_3']==True) | (df['Column_4']==True)
This works i think :
df["New_Column"] = df.Column_2 |df.Column_3| df.Column_4
Other option:
df["New_Column"] = df["Column_2"] |df["Column_3"]| df["Column_4"]
Be the following DataFrame in pandas:
Column_1 | Column_2 | Column_3 | Column_4 | Column_5 |
---|---|---|---|---|
82198 | True | False | False | red |
27498 | False | False | False | red |
84838 | False | False | True | red |
10498 | False | True | False | red |
I want to create a New_column
that has the value True if any of the columns Column_2
, Column_3
, Column_4
is True and False, if all 3 are false. Output example:
Column_1 | Column_2 | Column_3 | Column_4 | Column_5 | New_column |
---|---|---|---|---|---|
82198 | True | False | False | red | True |
27498 | False | False | False | red | False |
84838 | False | False | True | red | True |
10498 | False | True | False | red | True |
this might work:
df['new_column']=(df['Column_2']==True) | (df['Column_3']==True) | (df['Column_4']==True)
This works i think :
df["New_Column"] = df.Column_2 |df.Column_3| df.Column_4
Other option:
df["New_Column"] = df["Column_2"] |df["Column_3"]| df["Column_4"]