Pandas 问题,使用 "where" 方法时 0 转换为 NaN

Pandas problem, 0 transformed to NaN when use "where" method

我遇到了一个我不明白的问题:

>>> Y.isnull().values.any()
False
>>> Y.where(Y == 0).isnull().values.any()
True

我不明白 NaN 值怎么会出现在第二个结果中。 Y 有一个数据类型 = int64

有什么想法吗?非常感谢!

查看 pandas.DataFrame.where 的行为: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.where.html

它将不符合条件的值替换为某些值。默认情况下,某些东西不是布尔值,它是 1nan。这意味着通过执行 Y.where(Y == 0) 你正在创建 nan 值而不是所有不为 0 的值。因此第二行中的 nan 值。