I have a dataframe like this
ID Q001 Q002 Q003 Q004 Q005 Q006 Q007 Q008 Win
A 1 1 1 1 1 1 1 1 Yes
B 0 1 0 1 0 1 0 1 No
C 0 1 0 1 0 1 0 1 No
D 1 1 0 1 1 1 1 1 Yes
E 1 1 0 1 1 1 1 1 Yes
F 1 1 0 1 1 1 1 1 Yes
G 0 0 1 0 0 0 0 0 No
H 0 0 0 0 0 0 0 0 No
I 1 0 1 0 1 0 1 0 No
In the above dataframe, I want to create the colum ‘Win’ and assign the values ‘Yes’ if the sum of Q001 and Q002 is equal or higher than 2 and ‘No’, if lower than 2. How can I do this in Python?
>Solution :
Use np.where()
to return a value conditional on other columns.
df['Win'] = np.where(df['Q001'] + df['Q002'] >= 2, 'Yes', 'No')