Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 逻辑运算符&;不起作用,但';和';作品_Python_Pandas - Fatal编程技术网

Python 逻辑运算符&;不起作用,但';和';作品

Python 逻辑运算符&;不起作用,但';和';作品,python,pandas,Python,Pandas,我对pandas中的逻辑运算符有问题。 如果我尝试: list1=['x','y'] if st1=='A' & str2 not in list1: #do stuff 我得到: unsupported operand type(s) for &: 'str' and 'bool'", u'occurred at index 0 但这是可行的:为什么 if st1=='A' and str2 not in list1: #do stuff 我所做的只是更改和。&和和在Py

我对pandas中的逻辑运算符有问题。 如果我尝试:

list1=['x','y']
if st1=='A' & str2 not in list1: #do stuff
我得到:

unsupported operand type(s) for &: 'str' and 'bool'", u'occurred at index 0
但这是可行的:为什么

if st1=='A' and str2 not in list1: #do stuff

我所做的只是更改和。

&
在Python中不是一回事-
&
是位运算符,
是逻辑运算符。请参阅前面的答案和,以及维基百科中关于位运算的内容

在pandas中,您可以在选择数据帧子集时使用
&
进行逻辑操作,例如:

df = pd.DataFrame(data={"col1":[1,2,3], "col2":[2,3,4]})
df[(df["col1"]>1) & (df["col2"]<4)] # Selects second row based on boolean criteria
df=pd.DataFrame(数据={“col1”:[1,2,3],“col2”:[2,3,4]})

df[(df[“col1”]>1)和(df[“col2”]
在Python中不是一回事-
&
是位运算符,
是逻辑运算符。请参阅前面的答案和,以及维基百科关于位运算的内容

在pandas中,您可以在选择数据帧子集时使用
&
进行逻辑操作,例如:

df = pd.DataFrame(data={"col1":[1,2,3], "col2":[2,3,4]})
df[(df["col1"]>1) & (df["col2"]<4)] # Selects second row based on boolean criteria
df=pd.DataFrame(数据={“col1”:[1,2,3],“col2”:[2,3,4]})

df[(df[“col1”]>1)和(df[“col2”]您可能希望
st1==“A”而不是
=`对不起,这就是我所做的…错误仍然存在。您可能希望
st1==“A”而不是
=`对不起,这是我所做的…错误仍然存在。