gpt4 book ai didi

python - np.where() 如果条件失败则什么都不做

转载 作者:太空宇宙 更新时间:2023-11-03 15:42:29 25 4
gpt4 key购买 nike

我有一个来 self 的数据框的样本:

       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 列中的值,如果条件失败,函数什么都不做:

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

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

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

^
SyntaxError: invalid syntax

有什么替代方案可以实现同样的目标?

最佳答案

只返回列而不是 pass 这等同于当条件为 False 时什么都不做:

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

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

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

关于python - np.where() 如果条件失败则什么都不做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51782443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com