【发布时间】:2020-10-14 13:35:28
【问题描述】:
以下是我的数据框的简化版本
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)
我错过了什么?
【问题讨论】:
-
用括号分隔谓词:
np.where((ddd.x > 5) & (ddd.y > 5), 0, 1) -
变量z的值是多少?
标签: python pandas numpy operator-keyword