Articles → Numpy → Conditionally selecting the elements in an array in NumPy

Conditionally selecting the elements in an

array in NumPy






Example




import numpy as np

arr = np.arange(1,10)

# Check the array for numbers greater than 5
result =  arr > 5
print(arr[result])











Click to Enlarge






Click to Enlarge



import numpy as np

arr = np.arange(1,10)

print(arr[arr> 5])




import numpy as np

arr = np.arange(1,10)

# Check the array for even numbers
result =  arr % 2 == 0
even_num_array = arr[result]

# Check the array for odd numbers
result =  arr % 2 != 0
odd_num_array = arr[result]

print("Even array", even_num_array)
print("Odd array", odd_num_array)



Posted By  -  Karan Gupta
 
Posted On  -  Friday, March 29, 2019

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250