python - Pandas numpy.where() 使用 - 没有得到想要的结果

标签 python numpy pandas

我正在尝试根据 NaN 值将两列合并为第三列

df['code2'] = np.where(df['code']==np.nan, df['code'], df['code1'])

我只得到 code2 中 code1 列的值。结果如图所示 输出图像

enter image description here

请告诉我我写的代码有什么问题。谢谢

最佳答案

我想你需要isnull用于比较 NaN:

df['code2'] = np.where(df['code'].isnull(), df['code'], df['code1'])

Docs :

Warning

One has to be mindful that in python (and numpy), the nan's don’t compare equal, but None's do. Note that Pandas/numpy uses the fact that np.nan != np.nan, and treats None like np.nan.

In [11]: None == None
Out[11]: True

In [12]: np.nan == np.nan
Out[12]: False

So as compared to above, a scalar equality comparison versus a None/np.nan doesn’t provide useful information.

In [13]: df2['one'] == np.nan
Out[13]: 
a    False
b    False
c    False
d    False
e    False
f    False
g    False
h    False
Name: one, dtype: bool

关于python - Pandas numpy.where() 使用 - 没有得到想要的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36685963/

相关文章:

python - 如何使用 Python 打包一个大整数,将四个字符视为网络字节顺序中的无符号长整型,就像 Ruby 的 .pack ("N"那样?

python - 当 $env :path? 中定义了多个时,windows 如何决定使用哪个 python 可执行文件

python - 为了实现尾调用优化,jvm 必须牺牲什么?

python - 优化查找可以比较的数组对

python-3.x - 从奇数字典填充 Pandas 数据框

Python pandas 按多个索引范围切片数据框

python - Pandas 列由数组组成,使用 plt.imshow() 绘制

python - python中3D曲线的保形分段三次插值

python - 覆盖 Numpy 数组中的元素

Python Pandas : DataFrame modification with diagnal value = 0