Python 逻辑运算符&;不起作用,但';和';作品
我对pandas中的逻辑运算符有问题。 如果我尝试: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
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”而不是=`对不起,这是我所做的…错误仍然存在。