与 and 运算符的使用混淆

confusion with the use of and operator

下面是我的数据框的简化版本

df = pd.DataFrame(np.random.randint(1,10,(5,2)),columns=['x','y'])

如果 x 和 y 值均 > 5,则新列 'z' 的值为 0 else 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))