繁体   English   中英

改变顺序时不同的pandas DataFrame逻辑运算结果

[英]Different pandas DataFrame logical operation result when the changing the order

我的代码是这样的:

a = pd.DataFrame([np.nan, True])
b = pd.DataFrame([True, np.nan])
c = a|b
print(c)

当一个元素是 np.nan 时,我不知道逻辑运算结果的结果,但我希望它们无论如何都是相同的。 但我得到了这样的结果:

       0
0  False
1   True

为什么? 这是关于熊猫短路吗? 我搜索了熊猫的文档,但没有找到答案。

我的熊猫版本是 1.1.3

这是与np.nan相关的行为,而不是熊猫。 以下列例子为例:

print(True or np.nan)
print(np.nan or True)

输出:

True
nan

执行操作时, dtype 最终很重要,并且np.nan在 numpy 库中运行的方式是导致这种奇怪行为的原因。

为了解决这个问题,你可以用False或其他一些评估为False标记值填充 NaN 值(使用pandas.DataFrame.fillna() )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM