1

I have the following array:

array([[ 0.        , -1.22474487,  1.40182605],
       [ 1.22474487,  0.        , -0.53916387],
       [-1.22474487,  1.22474487, -0.86266219]])

What is the best way to parse each of its elements and assign a string depending on the value? For example: if value < 0 then set "LOW" else set "HIGH"?

1
  • 2
    You can't assign a string to a numpy array that already contains numbers. Do you want to create a new array or, list-of-lists with the results? Commented Feb 9, 2015 at 7:49

1 Answer 1

1

If a is your array use

import numpy as np

np.where(a<0,'LOW','HIGH')

Edit: When you have 3 choices you can do something like

b = np.where(a < 0.,'LOW','HIGH').astype('S7') 

c = np.where((-1. < a) & (a < 1.), 'MIDDLE',b)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.