Python 使用.loc在大熊猫中进行多重选择

Python 使用.loc在大熊猫中进行多重选择,python,python-2.7,pandas,Python,Python 2.7,Pandas,我已经成功地使用了.loc来分配已筛选数据集的一列,但是现在我需要使用两个筛选器筛选数据集,并得到以下错误。我正在尝试使用&操作符,就像您在进行筛选时一样 TypeError: cannot compare a dtyped [object] array with a scalar of type [bool] 代码: 期望输出: City State New Column 0 Arlington MA NAN 1 Arlin

我已经成功地使用了
.loc
来分配已筛选数据集的一列,但是现在我需要使用两个筛选器筛选数据集,并得到以下错误。我正在尝试使用
&
操作符,就像您在进行筛选时一样

TypeError: cannot compare a dtyped [object] array with a scalar of type [bool]
代码:

期望输出:

            City State  New Column
0      Arlington    MA         NAN
1      Arlington    TX          10
2         Dallas    TX         NaN

您需要将比较插入为
(df['State']=='TX')&(df['City']=='Arlington')
。在Python中,像
&
这样的位运算符比像
=

这样的比较运算符具有更高的优先级。您需要将比较括起来,作为
(df['State']='TX')&(df['City']='Arlington')
@BrenBarn,谢谢。为什么不作为答案发布,这样我就可以接受作为最佳答案?请注意,您没有使用
&
运算符“如果只是过滤,您会这样做”。如果尝试使用条件进行筛选而不是分配,则会出现相同的错误。
            City State  New Column
0      Arlington    MA         NAN
1      Arlington    TX          10
2         Dallas    TX         NaN