Numpy 'logical and' producing different type list

Question:

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]
Asked By: Mainland

||

Answers:

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]

Answered By: finman69

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

>>> op.tolist()

[False, True, True, True, True, False, False, False]
Answered By: L'Artiste