用pandas apply 创造新列条件运行遇到奇怪的现象

用pandas apply 创造新列条件运行遇到奇怪的现象

我的数据有两列,比如A列和B列吧。A列有PERMANENTLY WITHDRAWN和TEMPORARILY WITHDRAWN,以及其他值,B列有DRUG WITHDRAWN,DRUG INTERRUPTED和其他值。我想通过比较AB两列创建一个新列C,新列在一下条件下为"YES": 如果A列是PERMANENTLY WITHDRAWN而且B列是DRUG WITHDRAWN,或者A列是TEMPORARILY WITHDRAWN,B列是DRUG INTERRUPTED,或者A列和B列完全相等。我的代码如下:

def aeact_compare(x,y):

if x.strip() in ["PERMANENTLY WITHDRAWN\n","PERMANENTLY WITHDRAWN"] and y.strip() in ["DRUG WITHDRAWN\n","DRUG WITHDRAWN"]:

return "YES"

elif x.strip() in ["TEMPORARILY WITHDRAWN\n","TEMPORARILY WITHDRAWN"] and y.strip() in ["DRUG INTERRUPTED\n","DRUG INTERRUPTED"]:

return "YES"

elif x.strip() == y.strip():

return "YES"

else:

return "NO"

sdb_pdb_trt["Action Taken Match?"] = sdb_pdb_trt.apply(lambda x : aerel_compare(x['Action Taken'],x['Lastest Action Taken with TRT']),axis=1)

可是代码头两行的比较没起作用,请大家帮我看一下代码问题出在哪里?

以上是 用pandas apply 创造新列条件运行遇到奇怪的现象 的全部内容, 来源链接: utcz.com/p/937767.html

回到顶部