Home > Software engineering >  Numpy 'logical and' producing different type list
Numpy 'logical and' producing different type list

Time:08-29

I am doing a logical operation on a data frame column data using NumPy. I am checking if a row value is less than two compared to the preceding and successful row. I am getting output but different than what I expected

ad = pd.DataFrame({'a':[1,3,4,6,7,5,9,10]})
op=np.array(np.logical_and.reduce([ad['a'].\
                       diff(x).abs().le(2) for x in [1,-1]]))
print(op)

Present output:

[False  True  True  True  True False False False]

Expected output:

[False,  True,  True,  True,  True, False, False, False]

CodePudding user response:

You can use numpy.ndarray.tolist to convert the array to a list.

>>> op.tolist()

[False, True, True, True, True, False, False, False]

CodePudding user response:

This is just how the output for printing Numpy Arrays look. If you truly want the comma separated print output then you can just cast the original array as a list. otherwise these two outputs you've presented are the same

'''

ad = pd.DataFrame({'a':[1,3,4,6,7,5,9,10]})
op=np.array(np.logical_and.reduce([ad['a'].\
                   diff(x).abs().le(2) for x in [1,-1]]))
print(op)  
op = list(op)
print(op)

''''

output:

  • before list

[False True True True True False False False]

  • after list

[False, True, True, True, True, False, False, False]

  • Related