gpt4 book ai didi

python - 对两个 pandas 数据框列应用条件

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

我有这个数据框:

enter image description here

我想创建一个 ZIP 列,当 ZIP_x 为 NaN 时,它将获取 ZIP_y 的值;当 ZIP_x 不为 NaN 时,它将获取 ZIP_x 的值。

我尝试了这段代码:

dm["ZIP"]=numpy.where(dm["ZIP_x"] is numpy.nan, dm["ZIP_y"],dm["ZIP_x"])

但这给了我这个输出:

enter image description here

如您所见,ZIP 列似乎在其每个单元格中获取 ZIP_x 的值。

你知道如何实现我所追求的目标吗?

最佳答案

你想要这个:

dm["ZIP"]=numpy.where(dm["ZIP_x"].isnull(), dm["ZIP_y"],dm["ZIP_x"])

您不能使用 is== 来比较 NaNs

关于python - 对两个 pandas 数据框列应用条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35720611/

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