筛选布尔列时,pandas中的FutureWarning

2024-04-16 17:24:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我匹配了一个数据帧中的两个列,并在一个新的“bool”列中生成布尔值。 首先我写道:

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

但没用。在

然后我只写匹配的列

我的代码是

^{pr2}$

上面的代码匹配位于7和32的列,并将bool值放在第39列。在这里,我希望对数据进行切片,并选择值为false的行。 Data

我写道:

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

我得到了未来

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']

Tags: 数据代码infalsedfnewfordata
2条回答

可以直接比较列:

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

要筛选假值,请使用否定标志,即~

^{pr2}$

我不知道你的情况,当我使用下面的代码时,我得到了“未来警告:在未来,布尔数组likes将作为布尔数组索引处理”:

>>> 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])
>>>

我想这是两个“未来警告”之间的一些关系,希望我的情况能对你有所帮助。在

相关问题 更多 >