How to find the Index of value in Numpy Array ?
In this article, we are going to find the index of the elements present in a Numpy array.
Using where() Method
where() method is used to specify the index of a particular element specified in the condition.
Syntax: numpy.where(condition[, x, y])
Example 1: Get index positions of a given value
Here, we find all the indexes of 3 and the index of the first occurrence of 3, we get an array as output and it shows all the indexes where 3 is present.
All index value of 3 is: [2 7] First index value of 3 is: 2
Example 2: Print First Index Position of Several Values
Here, we are printing the index number of all values present in the values array.
index of first occurrence of each value: [1 2 9]
Example 3: Get the index of elements based on multiple conditions
Get the index of elements with a value less than 20 and greater than 12
Index of elements with value less than 20 and greater than 12 are: (array([ 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15], dtype=int64),)
Get the index of elements in the Python loop
Create a NumPy array and iterate over the array to compare the element in the array with the given array. If the element matches print the index.
element index : 5
Using ndenumerate() function to find the Index of value
It is usually used to find the first occurrence of the element in the given numpy array.
Using enumerate() function to find the Index of value
Here we are using the enumerate function and then checking the value with the target value.