#python #r #python-3.x #if-statement #conditional-statements
#питон #r #python-3.x #if-оператор #условные операторы
Вопрос:
Я был бы признателен, если бы вы могли помочь мне в этом, пожалуйста.
В чем проблема
ifelse(df2$Status=="Completion", "Completed",
ifelse(df2$Status=="Declined"|df2$Status=="DIP Declined","Declined",
ifelse(df2$Status=="Withdrawn"amp; df2$Decision == "Decline" | df2$Decision == "Refer" | df2$Decision == "Decline"),"Declined - Withdrawn",
ifelse(df2$Status=="Withdrawn","Withdrawn", "In-Flight"))))
Моя попытка
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")
)))))))
Сообщение об ошибке
TypeError: ufunc() takes from 2 to 3arguments but 4 were given
Спасибо!
Обновить
Это решения, которые сработали для меня (спасибо @Kay за первые два):
Способ 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"))))
Способ 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"
)
Способ 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)
Ответ №1:
Я думаю, что вам не хватает закрывающей скобки где-то в середине вашего кода. Эта строка
np.logical_and(df2.Status=="Withdrawn",np.logical_or(np.logical_or(df2["Decision"]=="Decline",df2["Decision"]=="Refer"),df2["Decision"]=="Decline")
должно быть:
np.logical_and(df2.Status=="Withdrawn",np.logical_or(np.logical_or(df2["Decision"]=="Decline",df2["Decision"]=="Refer"),df2["Decision"]=="Decline"))
Попробуйте это:
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"))))
Я обычно предпочитаю это, поскольку оно более читабельно:
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"
)