用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)
可是代码头两行的比较没起作用,请大家帮我看一下代码问题出在哪里?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论