« Numpy
numpy.where(condition to check, x, y)
Return x or y as elements based on condition check.
condition | array_like, bool |
x,y | x is returned if condition is True, y otherwise |
Examples : Updating data
We will create an array by using arange(). We will multiply each element by 3 if they are even numbers.
import numpy as np
ar=np.arange(6) #[0 1 2 3 4 5]
ar=np.where(ar%2==0,ar*3,ar)
print(ar)
Output ( updated the same array with new data )
[ 0 1 6 3 12 5]
Fill all elements by np.NaN if they are divisible by 5
import numpy as np
ar=np.arange(15)
ar=np.where(ar%5==0,np.NaN,ar)
print(ar)
output
[nan 1. 2. 3. 4. nan 6. 7. 8. 9. nan 11. 12. 13. 14.]
returns positions of elements where condition is True
import numpy as np
ar=np.array([12,2,7,1,9,3,11])
ar=np.where(ar>5)
print(ar)
Output ( Position of the elements where numbers are more than 5 )
(array([0, 2, 4, 6]),)
Using and to combine two conditions. Returns the position of elements satisfying the condition.
import numpy as np
ar=np.array([12,2,7,1,9,3,11])
ar=np.where((ar > 5) & (ar < 10))
print(ar)
print(ar[0][1])
Output
(array([2, 4]),)
4
Using OR to combine two conditions
import numpy as np
ar=np.array([12,2,7,1,4,3,11])
ar=np.where((ar > 5) | (ar %2==0))
print(ar)
Output
(array([0, 1, 2, 4, 6]),)
Using multidimensional arrays
np.where([[True, False], [True, True]],
[[5, 2], [13, 42]],[[9, 18], [73, 16]])
Output
array([[ 5, 18],
[13, 42]])
Nested np.where()
We can use nested np.where() condition checks ( like we do for case when condition checking in other languages). We will keep another np.where() when our first np.where() condition returns false.
Here is a solution we used to assign some numbers to another column ( allowed ) based on the value at dept column.
my_data['allowed']=np.where(my_data['dept']=='mktg',50,
np.where(my_data['dept']=='production',65,
np.where(my_data['dept']=='planning',45,np.nan)))
This is the part of a solution of Exercise No 3-4 , read the full exercise to understand the requirement.
Here my_data['allowed'] is assigned value of 50 if the my_data['dept'] column is equal to mktg, similarly this value is 65 for production and 45 for planning.
«Numpy
eye()
ones()
bincount()
linspace()