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

Eshaka
  • 974
  • 1
  • 14
  • 38

2 Answers2

1

Try:

inter_data = np.where((((j + 1) * 100) > inter_data) & (inter_data > (j * 100)), (j * 100) + 50, inter_data)
Aguy
  • 7,851
  • 5
  • 31
  • 58
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)
Dev Khadka
  • 5,142
  • 4
  • 19
  • 33