包含多个元素的数组的真值是二义性的.使用a.a any()或a.a all()



我得到了这个错误

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

设x为0到10之间的连续数值数组

dts1['NewCol'] = np.apply_along_axis(getbool,axis=0, arr=x)

为getbool函数,如果数值大于5则返回1,否则返回0

def getbool(x):
if x > 5: 
return 1
else: 
return 0

语法np.apply_along_axis(getbool,dts1,axis=0, arr=x)是错误的,正如@Derek所提到的。如果我正确理解了你的问题,下面的代码应该做你想做的:

import numpy as np
def getbool(x):
if x > 5: 
return 1
else: 
return 0

getbool = np.vectorize(getbool)
array = np.array([1, 2, 3, 1, 2, 5, 7, 9, 2, 7])
print(getbool(array))

输出为:[0 0 0 0 0 0 1 1 0 1]

最新更新