【问题标题】:Filtering for a column value and getting a future warning and a type error?过滤列值并获得未来警告和类型错误?
【发布时间】:2019-10-08 12:57:49
【问题描述】:

我正在尝试为 df 做一个简单的过滤器,但在下面出现此错误。

df_first = df_b[df_b['label']=='False']
df_second = df_b[df_b['label']=='True']

FutureWarning:元素比较失败;而是返回标量,但将来将执行元素比较 结果 = getattr(x, name)(y)

TypeError:无效类型比较

df_b 的样子

    id  label
0   92c False
1   f0  False
2   bb  True

不知道我错过了什么

谢谢!

【问题讨论】:

  • 该值是布尔值,而不是字符串。你需要df_b[df_b['label'] is False]df_b[df_b['label'] is True]
  • True 和 False 不是字符串

标签: python python-3.x pandas dataframe


【解决方案1】:

您必须通过布尔值TrueFalse 进行比较,而不是布尔值'True''False' 的字符串代表:

df_first = df_b[df_b['label']==False]
df_second = df_b[df_b['label']==True]

False 的comapre 的相同之处仅~ 反转掩码,True 比较省略==True

df_first = df_b[~df_b['label']]
df_second = df_b[df_b['label']]

【讨论】:

    猜你喜欢
    • 2020-02-15
    • 2017-04-11
    • 2015-09-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    相关资源
    最近更新 更多