-2

I got this error

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

Let be x an array of continuous numerical values between 0 and 10

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

Let be getbool a function which returns 1 if the numerical value is more than 5 and 0 otherwise

def getbool(x):
    if x > 5: 
        return 1
    else: 
        return 0
  • 1
    `np.apply_along_axis(getbool,dts1,axis=0, arr=x)` isn't going to produce the error message you showed. do you mean that you tried `np.apply_along_axis(getbool,arr=dts1,axis=0)` ? also can you include a sample of `dts1` so we can reproduce the error you're getting? you can copy and paste the output from `dts1.head().to_dict()` into your question – Derek O Dec 26 '22 at 21:09

1 Answers1

0

The syntax np.apply_along_axis(getbool,dts1,axis=0, arr=x) is wrong, as mentionned by @Derek. If i understood your question correctly, the following code should do what you want:

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))

The output is: [0 0 0 0 0 0 1 1 0 1]

Nonlinear
  • 684
  • 1
  • 12