Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create new column based on the sum of selected columns in python

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use np.where() to return a value conditional on other columns.

df['Win'] = np.where(df['Q001'] + df['Q002'] >= 2, 'Yes', 'No')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading