Skip to content
Related Articles

Related Articles

How to find the Index of value in Numpy Array ?
  • Last Updated : 21 Apr, 2021

In this article, we are going to find the index of the elements present in a NumPy array. The where() method is used to specify the index of a particular element specified in the condition.

Syntax:

numpy.where(condition)

Here, condition is the condition specified.

Example 1:



In this program, we are going to create an array with NumPy and display it. 

We can create an array with numpy by using the following syntax:

numpy.array([value1,value2,value3,.....,value n])

Python3




# import numpy package
import numpy as np
  
# create an numpy array with 1 
# to 10 elements
a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
  
print(a)


Output:

[ 1  2  3  4  5  6  7  8  9 10]

Finding the index of 3

Python3




# display index value
# of 3
print(np.where(a == 3))


Output:

(array([2], dtype=int64),)

Finding index value of 9



Python3




# display index value of 9
print(np.where(a == 9))


Output:

(array([8], dtype=int64),)

We can also get the index of elements based on multiple conditions. Those conditions are specified in the where() function.

Example 2:

Get the index of elements with a value less than 20 and greater than 12

Python3




# Create a numpy array from a list of numbers
# from 11 to 20
a = np.array([11, 12, 13, 14, 15, 16, 17, 15,
                11, 12, 14, 15, 16, 17, 18, 19, 20])
  
# Get the index of elements with value less 
# than 20 and greater than 12
print("The numbers index locations with the index of \
elements with value less than 20 and greater than 12 are ",
      np.where((a > 12) & (a < 20)))


Output:

The numbers index locations with the 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 with a value less than 20 and greater than 12

Python3






# Create a numpy array from a list of 
# numbers from 11 to 20
a = np.array([11, 12, 13, 14, 15, 16, 17, 15,
              11, 12, 14, 15, 16, 17, 18, 19, 20])
  
# Get the index of elements with value less 
# than 20 or greater than 12
print("The numbers index locations with the index of \
elements with value less than 20 or  greater than 12 are ",
      np.where((a > 12) | (a < 20)))


Output:

The numbers index locations with the index of elements with value less than 20 or  greater than 12 are  (array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16]),)

Example 3:

Display element index where values less than 20

Python3




# create numpy array elements
a = np.array([2, 3, 4, 5, 6, 45, 67, 34])
  
# display element index where values 
# less than 20
print("element index where values less than 20 : "
      np.where(a < 20))


Output:

element index where values less than 20 :  (array([0, 1, 2, 3, 4], dtype=int64),)

Example 4:

Display element index where values greater than 20

Python3




# create numpy array elements
a = np.array([2, 3, 4, 5, 6, 45, 67, 34])
  
# display element index where values 
# greater than 20
print("element index where values greater than 20 : "
      np.where(a > 20))


Output:

element index where values greater than 20 :  (array([5, 6, 7], dtype=int64),)

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :