You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何pandas的Series查询0存在但unique()返回结果中无0

问题原因解释

你遇到的输出矛盾来自in操作符在pandas Series和numpy数组上的判断逻辑差异:

  • 对dataset['LotFrontage']这个Series对象执行0 in dataset['LotFrontage']时,判断逻辑是检查0是否存在于Series的索引(index)中,和列本身的数值完全无关。如果你的数据集是默认的从0开始的整数索引,该语句必然返回True。
  • 对dataset['LotFrontage'].unique()返回的numpy数组执行0 in dataset['LotFrontage'].unique()时,判断逻辑才是检查0是否存在于列的去重数值中,这也和你确认的「列中没有0值」的结论匹配。
正确的列数值存在性校验写法

如果要判断LotFrontage列的数值中是否包含0,可使用以下任意一种写法:

  • 直接读取Series的数值数组判断:0 in dataset['LotFrontage'].values
  • 用pandas内置方法判断:dataset['LotFrontage'].eq(0).any()
  • 布尔索引判断:(dataset['LotFrontage'] == 0).any()

内容的提问来源于stack exchange,提问作者Ganesh Dagadi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 12:15:04