Find the index of value in Numpy Array using numpy.where()

How to find the index of value in Numpy array using numpy.where() in Python ?

NumPy or Numerical Python which is a general purpose array processing package and it is used for working with arrays. In python there numpy.where( ) function which is used to select elements from a numpy array, based on a condition and if the condition is satisfied we perform some operations.

Syntax : numpy.where(condition[ , x, y ])

Where

  • condition is a conditional expression which returns the Numpy array of bool
  • x,y are two optional arrays i.e either both are passed or not passed

In this article we will discuss about how to get the index of an element in a Numpy array (both 1D & 2D) using this function. So, let’s explore the concept well.

1. Find index of a value in 1D Numpy array :

Suppose array_one is an 1D Numpy array, So it will look like

array_one = np.array([10, 15, 20, 25, 30, 35, 40, 45, 50, 30, 60, 65, 70, 75, 80, 85, 30, 95])

Now, let’s find the index number of 30 in this Numpy array.

Program :

import numpy as np
# numpy array created with a list of numbers
array_one = np.array([10, 15, 20, 25, 30, 35, 40, 45, 50, 30, 60, 65, 70, 75, 80, 85, 30, 95])
# Get the index of elements with value 30
output = np.where(array_one == 30)
#printing the indices where the element 30 is present
print('Tuple of arrays returned : ', output)
print("Elements with value 30 exists at following indices", output[0], sep='\n')
Output :
Tuple of arrays returned :  (array([4, 9, 16]), )
Elements with value 30 exists at following indices 
[4 9 16]

As result is a tuple of arrays which contains the indices where value 30 exists in array array_one i.e. so it returned

(array([4, 9, 16]), )

And as a flat 1D array it returned

[4 9 16]

And if the element is not present in the Numpy array then returned array of indices will be empty. So let’s check that with an example

Program :

import numpy as np
# numpy array created with a list of numbers
array_one = np.array([10, 15, 20, 25, 30, 35, 40, 45, 50, 30, 60, 65, 70, 75, 80, 85, 30, 95])
# Get the index of elements with value 100
output = np.where(array_one == 100)
print('Tuple of arrays returned : ', output)
#printing the indices where the element 100 is present
#As 100 is not present in Numpy array i.e array_one so returned array with indices is empty 
print("Elements with value 30 exists at following indices", output[0], sep='\n')
Output :

Tuple of arrays returned : (array([  ]), )
Elements with value 30 exists at following indices
[  ]

2. Find index of a value in 2D Numpy array :

Suppose array_two is an 2D Numpy array, So it will look like

array_two = np.array([ [10, 15, 20], [25, 30, 35], [40, 45, 50], [30, 60, 65], [70, 75, 80], [85, 30, 95] ])

Now, let’s find the index number of 30 in this Numpy array.

Program :

import numpy as np
# numpy array created with a list of numbers
array_two = np.array([ [10, 15, 20], [25, 30, 35], [40, 45, 50], [30, 60, 65], [70, 75, 80], [85, 30, 95] ])
# Get the index of elements with value 30
output = np.where(array_two == 30)
#printing the indices where the element 30 is present
print('Tuple of arrays returned: ', output)
print("Elements with value 30 first exists at index:", output[0][0])
Output :
Tuple of arrays returned: (array([1, 3, 5]), array([1, 0, 1])) 
Elements with value 30 first exists at index: 1

Where,

First array [1, 3, 5] represents row indices where the element 30 is present

And the second array [1, 0, 1] represents column indices where the element 30 is present.

3. Get indices of elements based on multiple conditions in Numpy :

Example : 1

Get the indices of elements with value less than 20 and greater than 10.

Program :

import numpy as np
# numpy array created with a list of numbers
example_array = np.array([11, 6, 13, 8, 15, 16, 7, 15, 1, 2, 14, 19, 4, 20])
# Get the index of elements of value less than 20 and greater than 10
output = np.where((example_array  > 10) & (example_array  < 20))
print("Elements which are less than 20 and greater than 10 exists at indices",output, sep='\n')
Output :
Elements which are less than 20 and greater than 10 exists at indices 
(array([ 0, 2, 4, 5, 7, 10, 11]),)

Example : 2

Get the first index of an element in numpy array.

Program :

import numpy as np
# numpy array created with a list of numbers
example_array = np.array([15, 6, 13, 8, 15, 16, 7, 15, 1, 2, 14, 19, 4, 20])
# Get the index of elements with value less than 20 and greater than 10
output = np.where(example_array == 15)
if len(output) > 0 and len(output[0]) > 0:
    print('First Index of element with value 15 is ', output[0][0])
Output :
First Index of element with value 15 is 0

Example : 3

Get the indices of the elements which is less than equal to 2

Program :

import numpy as np 
arr = np.arange(10)
#prints the array
print(arr)
#prints indices of the elements which is less than equal to 2
print(arr[arr <= 2])
Output :
[0 1 2 3 4 5 6 7 8 9] 
[0 1 2]