Find index of value in NumPy array

In this post, we are going to learn to Find the index of the value in NumPy array 1D, 2D array. The numpy library function np. where() is used to find an index of value.The numpy. where(condition) condition as an argument returns the index of the element in the array.

1. Find index of value in 1D NumPy array

In this example, we will find the index of value 9 in the 1D NumPy array by passing a condition to numpy.where() method.It will return tuples of the array that contain indices(one for each axis), wherever value 9 exists. We can access indices by using indices[0] or indices[0][0] for the first occurrence.

```import numpy as np

nparr = np.array([3,6,9,12,15,18,21,24,27,30,9,9,9])

indices = np.where(nparr==9)

print(indices )

print('index of numpy value :',indices[0])
```

Output

```(array([ 2, 10, 11, 12], dtype=int32),)
index of element : [ 2 10 11 12]
```

2. Find index of value in 2D NumPy array

In this Python program example, we are finding the index of value 9 in a 2D array. The numpy.where(condition) will return a tuple of two arrays.

• In which,The first array tuples contain rowise indices for value 9.
• The second array tuple for column-wise indices for value 9.
• The zip() function to zip both arrays to get all indices row-wise and columnwise.
• The for loop to iterate over the zip arrays.
```import numpy as np

nparr = np.array([[3, 6, 9],
[12, 9, 18],
[21,9, 3],
[6,9 , 12]])

index = np.where(nparr==9)

listofIndices = list(zip(index[0], index[1]))
for indexes in listofIndices:
print(indexes)

```

Output

```(0, 2)
(1, 1)
(2, 1)
(3, 1)
```

3. Find index of values greater than

In this Python program example, we have gotten the indices of values that are greater than 9 and getting indices based on multiple conditions.

```import numpy as np

nparr = np.array([[3, 6, 9],
[12, 9, 18],
[21,9, 3],
[6,9 , 12]])

listofIndices = np.where(nparr >9)
for indexes in listofIndices:
print(indexes)

#access indices based on mutiple condition
muticondition = np.where((nparr >9) & (nparr<21))
for indexes in muticondition :
print('based on mutiple condition:'indexes)

```

Output

```[1 1 2 3]
[0 2 0 2]

based on mutiple condition:[1 1 3]
[0 2 2]
```

4. Find index of max value in NumPy Array

In this Python program, we are finding max values and their indices in numpy array rows-wise and column-wise.

```import numpy as np

nparr = np.array([[3, 6, 9],
[12, 9, 18],
[21,9, 3],
[6,9 , 12]])

#column wise max values and indexes
max_val_colwise = np.amax(nparr, axis=0)
maxVal_Index_colwise = np.argmax(nparr, axis=0)

print('max val column-wise:',max_val_colwise)
print('max val index colwise:',maxVal_Index_colwise)

#row wise max values and indexes
maxVal_ind_rowise = np.argmax(nparr, axis=1)
max_val_Rowwise = np.amax(nparr, axis=1)

print('max val index rowise:',max_val_Rowwise)

print('max val index rowise:',maxVal_ind_rowise)
```

Output

```max val column-wise: [21  9 18]
max val index colwise: [2 1 1]
max val index rowise: [ 9 18 21 12]
max val index rowise: [2 2 0 2]
```

5. Find index of min value in NumPy array rowise and colummnwise

In this Python program, we are finding min values and their indices in a numpy array rowise and column-wise.

```import numpy as np

nparr = np.array([[3, 6, 9],
[12, 9, 18],
[21,9, 3],
[6,9 , 12]])

#column wise max values and indexes
max_val_colwise = np.amin(nparr, axis=0)
maxVal_Index_colwise = np.argmin(nparr, axis=0)

print('max val column-wise:',max_val_colwise)
print('max val index colwise:',maxVal_Index_colwise)

#row wise max values and indexes
maxVal_ind_rowise = np.argmin(nparr, axis=1)
max_val_Rowwise = np.amax(nparr, axis=1)

print('max val index rowise:',max_val_Rowwise)

print('max val index rowise:',maxVal_ind_rowise)
```

6. First index of value in NumPy array

In this Python program example, We are getting the first index of the value in the NumPy array by numpy.where(condition) that we will use indices[0][0] for the first occurrence.

```import numpy as np

nparr = np.array([3,6,9,12,15,18,21,24,27,30,9,9,9])

index = np.where(nparr==9)

if len(index) > 0 and len(index[0]) > 0:
print('First Index of  value 9 is :', index[0][0])
```

Output

```First Index of  value 9 is : 2
```

Summary

In this post, we are going to learn how to find the index of the value in the NumPy array using numpy.where(condition) with examples.