Python 与and运算符的使用混淆

Python 与and运算符的使用混淆,python,pandas,numpy,operator-keyword,Python,Pandas,Numpy,Operator Keyword,下面是我的dataframe的简化版本 df = pd.DataFrame(np.random.randint(1,10,(5,2)),columns=['x','y']) 如果x和y值均大于5,则新列“z”的值将为0,否则为1 df[z] = np.where(ddd.x>5 and ddd.y>5,0,1) 然而,我得到了这个错误 ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool

下面是我的dataframe的简化版本

df = pd.DataFrame(np.random.randint(1,10,(5,2)),columns=['x','y'])
如果x和y值均大于5,则新列“z”的值将为0,否则为1

df[z] = np.where(ddd.x>5 and ddd.y>5,0,1)
然而,我得到了这个错误

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
即使我使用

df[z] = np.where(ddd.x>5 & ddd.y>5,0,1)

我遗漏了什么?

您需要对每种情况使用偏执,否则它将不起作用:

df[z] = np.where((ddd.x > 5) & (ddd.y > 5), 0, 1)

您需要对每个条件使用偏执,否则它将不起作用:

df[z] = np.where((ddd.x > 5) & (ddd.y > 5), 0, 1)

您只需要为每个条件添加括号。看看这个:

df[z] = np.where((ddd.x>5) & (ddd.y>5,0,1))

您只需要为每个条件添加括号。看看这个:

df[z] = np.where((ddd.x>5) & (ddd.y>5,0,1))

使用括号分隔谓词:
np.where((ddd.x>5)和(ddd.y>5),0,1)
变量z的值是多少?使用括号分隔谓词:
np.where((ddd.x>5)和(ddd.y>5),0,1)
变量z的值是多少?