与 NaN 相等的元素比较 [英] Element-wise comparison with NaNs as equal

查看:48
本文介绍了与 NaN 相等的元素比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我运行以下代码:

dft1 = pd.DataFrame({'a':[1, np.nan, np.nan]})
dft2 = pd.DataFrame({'a':[1, 1, np.nan]})
dft1.a==dft2.a

结果是

0     True
1    False
2    False
Name: a, dtype: bool

我怎样才能使结果成为

0     True
1    False
2     True
Name: a, dtype: bool

即, np.nan == np.nan 评估为 True.

I.e., np.nan == np.nan evaluates to True.

我认为这是基本功能,我一定是问了一个重复的问题,但我花了很多时间在 SO 或 Google 中搜索却找不到.

I thought this is basic functionality and I must be asking a duplicate question, but I spent a lot of time search in SO or in Google and couldn't find it.

推荐答案

想不到一个函数已经为你做了这件事(奇怪)所以你可以自己做:

Can't think of a function that already does this for you (weird) so you can just do it yourself:

dft1.eq(dft2) | (dft1.isna() & dft2.isna())

       a
0   True
1  False
2   True

注意括号的存在.在 Pandas 中使用重载的按位运算符时需要注意优先级.

Note the presence of the parentheses. Precedence is a thing to watch out for when working with overloaded bitwise operators in pandas.

另一种选择是使用 np.nan_to_num,如果您确定两个 DataFrame 的索引和列相同,则此结果有效:

Another option is to use np.nan_to_num, if you are certain the index and columns of both DataFrames are identical so this result is valid:

np.nan_to_num(dft1) == np.nan_to_num(dft2)

array([[ True],
       [False],
       [ True]])

np.nan_to_num 用一些填充值填充 NaN(数字为 0,字符串数组为 'nan').

np.nan_to_num fills NaNs with some filler value (0 for numeric, 'nan' for string arrays).

这篇关于与 NaN 相等的元素比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆