【问题标题】:FutureWarning in pandas when filtering on Boolean column在布尔列上过滤时 Pandas 中的 FutureWarning
【发布时间】:2016-06-11 20:44:09
【问题描述】:

我匹配了 DataFrame 中的 2 列,并在新的“bool”列中生成布尔值的结果。 首先我写道:

df_new = df[[7 for 32 in df if df 39 == 'False']]

但是没有用。

然后我只写了匹配列

我的代码是

df['bool'] = (df.iloc[:, 7] == df.iloc[:, 32])

上面的代码匹配位于 7 和 32 的列,并将 bool 值放在第 39 列。在此我想对数据进行切片并选择具有 false 值的行。 Data

我写道:

df_filtered = df[df['bool'] == 'False']

但我得到了 Future 错误

c:\python34\lib\site-packages\pandas\core\ops.py:714: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison result = getattr(x, name)(y)

不知道我做错了什么。 我也试过了

df[df[39] == 'False']

【问题讨论】:

  • 您能发布小样本数据和预期结果吗? By the way 'False' is string dtype and False is bool
  • 我刚刚附上了数据集和预期结果,包括我尝试过的结果。
  • WoodChopper : 请检查有问题的数据链接...刚刚添加

标签: python pandas filtering


【解决方案1】:

您可以直接比较列:

df = pd.DataFrame({'a': [1, 2, 3], 'b': [1, 3, 2]})

df['Bool'] = df.a == df.b

>>> df
   a  b   Bool
0  1  1   True
1  2  3  False
2  3  2  False

要过滤 False 值,请使用否定标志,即~

>>> df[~df.Bool]
   a  b   Bool
1  2  3  False
2  3  2  False

【讨论】:

  • 是的,直到这里我有了结果,但我需要仅根据 False 值进一步切片此数据。 a b 布尔值 1 2 3 假 2 3 2 假
  • Alexander - 感谢您的回复,但我没有得到前面提到的结果...我的原始代码是:Delta_check[~Delta_check.bool] 我得到 TypeError: bad operand type for unary ~: '方法'
  • 您的专栏的确切名称是什么?
  • bool 是我的专栏的确切名称
  • bool是DataFrame方法,所以需要使用显式引用:df[~df['bool']]
【解决方案2】:

我不知道你的情况,当我使用以下代码时,我得到“FutureWarning:将来,布尔数组喜欢将被处理为布尔数组索引”:

>>> import numpy as np
>>> test=np.array([1,2,3])
>>> test3=test[[False,True,False]]
__main__:1: FutureWarning: in the future, boolean array-likes will be handled as a boolean array index
>>> test3=test[np.array([False,True,False])]
>>> test3
array([2])
>>>

我认为这些是两个“FutureWarning”之间的一些关系,希望我的情况可以帮助到你。

【讨论】:

    猜你喜欢
    • 2019-02-05
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 2021-03-22
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    相关资源
    最近更新 更多