如果条件失败,np.where()则不执行任何操作

编程入门 行业动态 更新时间:2024-10-27 19:19:06
本文介绍了如果条件失败,np.where()则不执行任何操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个数据框示例:

Created Insert Time MatchKey In Previous New Type 18593 2016-08-12 2018-02-19 LXGS090393APIN040640 No New Existing 5517 2016-08-12 2018-02-19 LIN380076CI166203726 No New Existing 2470 2018-02-12 2018-02-19 CI164414649APIN160672 No New Existing 13667 2016-08-12 2018-02-19 LIN257400APIN015446 Yes New Existing 10998 2016-08-12 2018-02-19 LXSV225786APIN158860 Yes New Existing 20149 2016-08-12 2018-02-19 LIN350167APIN158284 Yes New Existing 20143 2016-08-12 2018-02-19 LIN350167APIN161348 Yes New Existing 30252 2016-08-12 2018-02-19 LXGS120737APIN153339 Yes New Existing 12583 2016-08-09 2018-02-19 WIN556410APIN157186 Yes New Existing 28591 2018-05-03 2018-02-19 CI195705185APIN009076 No New Created

我想用以下方式替换 New Type 列中的值:如果条件失败,该函数将不执行任何操作:

I wanted to replace values in New Type column in a way that if the condition is failed the function does nothing:

current['New Type'] = np.where(current['In Previous']=='Yes','In Previous',pass)

但显然会导致语法错误,因为np.where()无法处理 pass :

but apparently it causes a syntax error as np.where() doesn't handle pass:

File "<ipython-input-9-7f68cda12cbe>", line 1 current['New Type'] = np.where(current['In Previous']=='Yes','In Previous',pass) ^ SyntaxError: invalid syntax

要实现相同目标,有什么替代方法?

What would be an alternative to achieve the same?

推荐答案

只返回该列而不是pass,这与在条件为False时不执行任何操作一样:

Just return the column instead of pass this is the same as doing nothing when the condition is False:

current['New Type'] = np.where(current['In Previous']=='Yes','In Previous',current['New Type'] )

或者您也可以屏蔽这些行:

Or you can just mask those rows:

current['New Type'] = current.loc[current['In Previous']=='Yes', 'In Previous']

更多推荐

如果条件失败,np.where()则不执行任何操作

本文发布于:2023-10-28 18:50:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537474.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条件   操作   np

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!