How to find the Index of value in Numpy Array ?
Last Updated :
09 Aug, 2024
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.
Python
# import numpy package
import numpy as np
# create an numpy array
a = np.array([1, 2, 3, 4, 8, 6, 7, 3, 9, 10])
# display index value of 3
print("All index value of 3 is: ", np.where(a == 3)[0])
print("First index value of 3 is: ",np.where(a==3)[0][0])
Output:
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.
Python
# import numpy package
import numpy as np
# create an numpy array
a = np.array([1, 2, 3, 4, 8, 6, 2, 3, 9, 10])
values = np.array([2, 3, 10])
# index of first occurrence of each value
sorter = np.argsort(a)
print("index of first occurrence of each value: ",
sorter[np.searchsorted(a, values, sorter=sorter)])
Output:
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
Python
# Create a numpy array
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("Index of elements with value less\
than 20 and greater than 12 are: \n",
np.where((a > 12) & (a < 20)))
Output:
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.
Python
import numpy as np
# create numpy array elements
a = np.array([2, 3, 4, 5, 6, 45, 67, 34])
# display element index where value = 45
index_of_element = -1
for i in range(a.size):
if a[i] == 45:
index_of_element = i
break
if index_of_element != -1:
print("element index : ", index_of_element)
else:
print("The element not present")
Output:
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.
Python
import numpy as np
def ind(array, item):
for idx, val in np.ndenumerate(array):
if val == item:
return idx
# If no item was found return None, other return types might be a problem due to
# numbas type inference.
a = np.array([11, 12, 13, 14, 15, 16, 17, 15,
11, 12, 14, 15, 16, 17, 18, 19, 20])
print(ind(a,11))
Output:
(6,)
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.
Python
import numpy as np
a = np.array([11, 12, 13, 14, 15, 16, 17, 15,
11, 12, 14, 15, 16, 17, 18, 19, 20])
print(next(i for i, x in
enumerate(a) if x == 17))
Output:
6
Similar Reads
Find the nearest value and the index of NumPy Array In this article, let's discuss finding the nearest value and the index in an array with Numpy. We will make use of two of the functions provided by the NumPy library to calculate the nearest value and the index in the array. Those two functions are numpy.abs() and numpy.argmin(). Example Input Arra
3 min read
How to remove NaN values from a given NumPy array? In this article, we are going to learn how to remove Nan values from a given array. Nan values are those values that do not have a specific value associated with them or they are different from the type of values that are to be used in the declared array. There are basically three approaches with sl
3 min read
How to get values of an NumPy array at certain index positions? Sometimes we need to remove values from the source Numpy array and add them at specific indices in the target array. In NumPy, we have this flexibility, we can remove values from one array and add them to another array. We can perform this operation using numpy.put() function and it can be applied t
4 min read
Finding the k smallest values of a NumPy array In this article, let us see how to find the k number of the smallest values from a NumPy array. Examples: Input: [1,3,5,2,4,6] k = 3 Output: [1,2,3] Method 1: Using np.sort() . Approach: Create a NumPy array.Determine the value of k.Sort the array in ascending order using the sort() method.Print th
2 min read
How to get the n-largest values of an array using NumPy? Let's see the program for how to get the n-largest values of an array using NumPy library. For getting n-largest values from a NumPy array we have to first sort the NumPy array using numpy.argsort() function of NumPy then applying slicing concept with negative indexing. Syntax: numpy.argsort(arr, ax
2 min read
How to Change a Single Value in a NumPy Array NumPy arrays are a fundamental data structure in Python, widely used for scientific computing and data analysis. They offer a powerful way to perform operations on large datasets efficiently. One common task when working with NumPy arrays is changing a single value within the array. This article wil
6 min read
How to Calculate the Mode of NumPy Array? The goal here is to calculate the mode of a NumPy array, which refers to identifying the most frequent value in the array. For example, given the array [1, 1, 2, 2, 2, 3, 4, 5], the mode is 2, as it appears most frequently. Let's explore different approaches to accomplish this. Using scipy.stats.mod
2 min read
How to Find Index of Element in Array in MATLAB? In MATLAB, the arrays are used to represent the information and data. You can use indexing to access the elements of the array. Â In MATLAB the array indexing starts from 1. To find the index of the element in the array, you can use the find() function. Using the find() function you can find the indi
4 min read
How to skip every Nth index of NumPy array ? NumPy arrays offer efficient numerical operations and data storage. When working with large arrays, sometimes it's necessary to skip specific indices for optimization or data processing purposes. This article will show how to skip every Nth index of the NumPy array. There are various ways to access
4 min read
How to get the indices of the sorted array using NumPy in Python? We can get the indices of the sorted elements of a given array with the help of argsort() method. This function is used to perform an indirect sort along the given axis using the algorithm specified by the kind keyword. It returns an array of indices of the same shape as arr that would sort the arra
2 min read