0

i have asked something slimier before for 1 condition and it was a simple answer and works great. change every value in a numpy array with a condition

now i am trying to find a way to do the same with two conditions.

 for j in range(5,45):
     # inter_data[(j*100) <= inter_data < ((j+1)*100)] = (j*100) + 50
     inter_data = np.where(((j + 1) * 100) > inter_data > (j * 100), (j * 100) + 50, inter_data)

i tried using the same technique and it doesn't work. seems like this can only work with one condition.

it gives the following error for two conditions

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

2
  • 1
    Check out: stackoverflow.com/questions/16343752/… Commented Sep 30, 2019 at 2:38
  • a>b>c works python scalars, but not for numpy arrays. Commented Sep 30, 2019 at 2:55

2 Answers 2

1

Try:

inter_data = np.where((((j + 1) * 100) > inter_data) & (inter_data > (j * 100)), (j * 100) + 50, inter_data)
Sign up to request clarification or add additional context in comments.

Comments

1

you can use logical and opertion & to do this like below using vectorizaion (with out using python loop)


import numpy as np
ar = np.arange(5,45)
inter_data = np.arange(100, 100+40*100, 100)


cond = (ar+1) * 100 > inter_data
cond &= (ar) * 100 < inter_data

np.where(cond, ar*100+50, inter_data)

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.