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

Is there a way to save a new column based on several conditions in python?

My problem is that I want to create a new column based on two conditions: if type == condition, and name == depression. It is necessary that two conditions like these are satisfied at the same time.

Here is the demonstration data:

#for demonstration
import pandas as pd

example = {
"ID": [1, 2,3, 4, 5],
"type": ["condition", "temperature", "condition", "condition", "status"],
"name":  ["depression", "high", "fatigue", "depression", "positive"],
}

#load into df:
example = pd.DataFrame(example)

print(example) 

The result I expect to get look like this:

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

#for demonstration
import pandas as pd

result = {
"ID": [1, 2,3, 4, 5],
"type": ["condition", "temperature", "condition", "condition", "status"],
"name":  ["depression", "high", "fatigue", "depression", "positive"],
"depression":["yes", "no","no","yes", "no"]
}

#load into df:
result = pd.DataFrame(result)

print(result) 

The most closest to the solution what I’ve tried was this:

example["depression"]= example[example.type=="condition"].name == "depression"  

and as output it gives

enter image description here

But this is not what I want, because it classify others as NAN, and gives True or False as values. What I want is in the result df above.

Could you please help me to find out how to deal with it?
Thanks in advance!

>Solution :

You can use np.where to do that:

example["depression"] = np.where(example.type.eq("condition") & example.name.eq("depression"), "yes", "no")

print(example)
-------------------------------------------------------
    ID  type        name        depression
0   1   condition   depression  yes
1   2   temperature high        no
2   3   condition   fatigue     yes
3   4   condition   depression  yes
4   5   status      positive    no
-------------------------------------------------------

It returns you the second argument (‘yes’) for all True conditions and the third argument (‘no’) else. The conditions are specified in the first argument.

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