Felipe Araya Olea
Felipe Araya Olea

Reputation: 243

Translate these multiple conditional from R to Python

I would appreciate if you could help me on this one, please.

The Problem

ifelse(df2$Status=="Completion", "Completed",
    ifelse(df2$Status=="Declined"|df2$Status=="DIP Declined","Declined",
        ifelse(df2$Status=="Withdrawn"& df2$Decision == "Decline" | df2$Decision == "Refer" | df2$Decision == "Decline"),"Declined - Withdrawn",
             ifelse(df2$Status=="Withdrawn","Withdrawn", "In-Flight"))))

My Attempt

np.where(df2["Status"]=="Completion", "Completed",\
    (np.where(np.logical_or(df2.Status=="Declined",df2.Status=="DIP Declined"),"Declined",\
        (np.where(np.logical_and(df2.Status=="Withdrawn",np.logical_or(np.logical_or(df2["Decision"]=="Decline",df2["Decision"]=="Refer"),df2["Decision"]=="Decline"),"Declined - Withdrawn",
            (np.where(df2.Status=="Withdrawn","Withdrawn","In-Flight")
 )))))))

Error message

TypeError: ufunc() takes from 2 to 3arguments but 4 were given

Thank you!

Update

These are the solutions that worked for me (credits to @Kay for the first two):

Method 1

np.where(df2["Status"]=="Completion", "Completed",
np.where(np.logical_or(df2.Status=="Declined",df2.Status=="DIP Declined"),"Declined",
    np.where(np.logical_and(df2.Status=="Withdrawn",np.logical_or(np.logical_or(df2["Decision"]=="Decline",df2["Decision"]=="Refer"),df2["Decision"]=="Decline")),"Declined - Withdrawn",
        np.where(df2.Status=="Withdrawn","Withdrawn","In-Flight"))))

Method 2:

np.select(
[
    df2["Status"]=="Completion",
    np.logical_or(df2.Status=="Declined",df2.Status=="DIP Declined"),
    np.logical_and(df2.Status=="Withdrawn",np.logical_or(np.logical_or(df2["Decision"]=="Decline",df2["Decision"]=="Refer"),df2["Decision"]=="Decline")),
    df2.Status=="Withdrawn"
],
["Completed","Declined","Declined - Withdrawn","Withdrawn"],
default="In-Flight"

)

Method 3:

def func(df,status,decision):
    var = "Completed" if df[status] == "Completion" else "Declined"\
    if df[status] == "Declined" or df[status] == "DIP Declined" else "Declined - Withdrawn"\
    if df[status]=="Withdrawn" and ((df[decision]=="Decline" or df[decision]=="Refer") or df[decision]=="Decline") else "Withdrawn"\
    if df[status]=="Withdrawn" else "In-Flight"
    
    return var

df.apply(lambda x: func(x,"Status","Decision"),axis=1)

Upvotes: 0

Views: 25

Answers (1)

Kay
Kay

Reputation: 2332

I think you are missing a closing bracket somewhere in the middle of your code. This line

np.logical_and(df2.Status=="Withdrawn",np.logical_or(np.logical_or(df2["Decision"]=="Decline",df2["Decision"]=="Refer"),df2["Decision"]=="Decline")

should be:

np.logical_and(df2.Status=="Withdrawn",np.logical_or(np.logical_or(df2["Decision"]=="Decline",df2["Decision"]=="Refer"),df2["Decision"]=="Decline"))

Try this:

np.where(df2["Status"]=="Completion", "Completed",
    np.where(np.logical_or(df2.Status=="Declined",df2.Status=="DIP Declined"),"Declined",
        np.where(np.logical_and(df2.Status=="Withdrawn",np.logical_or(np.logical_or(df2["Decision"]=="Decline",df2["Decision"]=="Refer"),df2["Decision"]=="Decline")),"Declined - Withdrawn",
            np.where(df2.Status=="Withdrawn","Withdrawn","In-Flight"))))

I usually prefer this as it is more readable:

np.select(
    [
        df2["Status"]=="Completion",
        np.logical_or(df2.Status=="Declined",df2.Status=="DIP Declined"),
        np.logical_and(df2.Status=="Withdrawn",np.logical_or(np.logical_or(df2["Decision"]=="Decline",df2["Decision"]=="Refer"),df2["Decision"]=="Decline")),
        df2.Status=="Withdrawn"
    ],
    ["Completed","Declined","Declined - Withdrawn","Withdrawn"],
    default="In-Flight"
)

Upvotes: 1

Related Questions