# Python NumPy Indexing – Detailed Guide

In this Python tutorial, we will learn how to get the index number in the NumPy array  Python. Also, we will cover these topics.

• Python NumPy indexing array
• Python numpy index of value
• Python NumPy index of max value
• Python NumPy index of element
• Python NumPy index of minimum
• Python NumPy index condition
• Python NumPy index function
• Python NumPy index list
• Python NumPy index search
• Python NumPy index where
• Python NumPy get index where value is true
• Python NumPy sort with index
• Python NumPy exclude index
• Python NumPy return index of max value
• Python NumPy remove index
• Python NumPy random index
• ‘Python NumPy reset index
• Python NumPy unique index
• Python NumPy get index of min value
• Python NumPy logical indexing
• Python NumPy boolean indexing
• Python np.ravel_multi_index

## Python NumPy indexing

• In this section, we will discuss how to find the index of a value in the NumPy array in Python.
• To perform this particular task we are going to use the slicing() method. In Python, the slicing method is used to signify how to slice a sequence in an array. For example, we have an array of 1000 elements and now we want to select only particular values from an array, then we can easily use the slicing method and get the selected values from an array.
• This method takes three parameters and within this function, we have to pass the stop, step and start parameters to the built-in slice function.

Let’s have a look at the Syntax and understand the working of slicing

``````slice
(
start,
stop,
step
)``````

Example:

``````import numpy as np

new_arr = np.arange(16)
result = slice(2)
print("Slicing elements:",new_arr[result])``````

In the above code, we created a arrange function and then define a slice object with the value ‘2’. After that, the slice object is set in an array. Once you will print ‘new_arr'[result] then the output will display the [0,1] slicing elements.

Here is the Screenshot of the following given code

Also, check: Python NumPy 2d array

## Python NumPy indexing array

• In this Program, we will discuss how to get the indexing of a NumPy array in Python.
• To do this task we are going to use the array condition[] in which we will specify the index number and get the element in an output.
• In this example, we will create a NumPy array by using the function np.array(). After that, we are going to use the print statement and set new_array [1,3] as an argument.

Source Code:

``````import numpy as np

new_array = np.array([[12,23,65,16,82], [54,16,17,23,98]])

print("Indexing element:", new_array[1, 3]) ``````

Here is the implementation of the following given code

As you can see in the Screenshot the output displays the 4th element on 2nd row.

## Python numpy index of value

• In this section, we will discuss how to get the index number of the Numpy array element in Python.
• By using the np.where() function, we can easily find the index number of a given value in an array. In Python, the np.where() function is used for selecting the elements from a given array and it always returns the indices of elements.
• For example, we have an array with multiple values and our condition could be x and y arguments are not passed only condition parameter is passed and it will return a new array after filtering based on condition.

Let’s have a look at how we can apply this function in our program

Syntax:

``````numpy.where
(
condition
[,
x,
y
]
)``````

Example:

Let’s take an example and check how to get the index value in the NumPy array

Source Code:

``````import numpy as np

new_array = np.array([23, 27,31, 47, 36, 78, 85, 18, 34, 39])

new_output = np.where(new_array > 36)
print("Index value:",new_output)``````

Here is the Output of the following given code

## Python NumPy index of max value

• In this section, we will discuss how to get the index of max value in the NumPy array in Python.
• To perform this particular task we are going to use the np.argmax() function. In Python, this function is used to get the index of maximum number and it retrieves the index associated with the maximum value.
• This method takes an array as an argument along with an axis and this function is available in the NumPy package module.

Let’s have a look at the Syntax and understand the np.argmax() function

``````numpy.argmax
(
a,
axis=None,
out=None
)``````
• It consists of a few parameter
• a: This parameter indicates the input array
• axis: This is an optional parameter and by default it takes None value.

Example:

``````import numpy as np

new_array = np.array([23,45,3,6,12,34,56,78,97,34,2,5,])
print("Creation of array:",new_array)
result= new_array.argmax(axis=0)
print("index no of max value:",result)``````

In the above code, we imported the numpy library and then initialize an array by using the np.array() function. After that, we declared a variable ‘result’ and assign np.argmax() function along with axis=0. Once you will print ‘result’ then the output will display the index number of maximum value

You can refer to the below Screenshot

## Python NumPy index of element

• Let us see how to find the index of elements in the NumPy array Python.
• In this program, we are going to use the concept of index() method and it will return the position of a selected element.
• We have also used them to.list() method for converting the array into a list. Once you will print ‘result’ then the output will display the index number of ’89’.

Example:

``````import numpy as np

new_arr = np.array([14,21,36,89,167,234,987,345,189])
result= new_arr.tolist().index(89)
print("Index number of selected element:",result)``````

You can refer to the below Screenshot

## Python NumPy index of minimum

• In this section, we will discuss how to get the index number of the minimum value in NumPy array Python.
• To do this task we are going to use the np.argmin() function and it will return the index number of minimum value. In Python the np.argmin() function is available in NumPy package and this method is quite similar to numpy.minimum() function but instead of returning the minimum value. It will always return the index number of minimum value.

Syntax:

Here is the Syntax of numpy.argmin() function

``````numpy.argmin
(
a,
axis=None,
out=None
)``````

Source Code:

``````import numpy as np

new_values = np.array([17,56,14,19,23,78,34,15,8,7,14,19])
print("Creation of array:",new_values)
new_output= new_values.argmin(axis=0)
print("index number of minimum value:",new_output)``````

In the above code, we imported the numpy library and then initialized an array by using the np.array() function. Now declare a variable ‘new_output’ and assign np.argmin() function along with axis=0.

Here is the execution of the following given code

As you can see in the Screenshot the output displays the index number ‘9’

## Python NumPy index condition

• In this Program, we will discuss how to get the index number of elements by using conditions in numpy array Python.
• To do this task we are going to use the np.where() function. In Python, the np.where() function is used for selecting the elements from a given array and it always returns the indices of elements.

Syntax:

Here is the Syntax of numpy.where() function

``````numpy.where
(
condition
[,
x,
y
]
)``````

Example:

``````import numpy as np

new_values = np.array([78, 13, 34, 28, 86, 58, 64, 18])

final_result = np.where(new_values < 58)
print("Index value based on condition:",final_result)``````

In the above program, we used the numpy.where() function and it will check the condition if the array element is less than ’58’ then it will display the index number of that element.

Here is the implementation of the following given code

## Python NumPy index function

• Let us see how to use the index() function in NumPy array Python.
• In this example, we are going to use the index function for getting the index value of the specific element. We have also used them to.list() method for converting the array into a list.

Example:

``````import numpy as np

values = np.array([22,14,17,18,8,17,74,28,109])
new_output= values.tolist().index(17)
print("Index number:",new_output)``````

You can refer to the below Screenshot

As you can see in the Screenshot the output displays the index number ‘2’.

## Python NumPy index list

• In this section, we will discuss how to get the index list by using NumPy Python.
• To do this task we are going to use the array condition and pass an integer value to it. Once you will print ‘result’ then the output will display the index list number.

Example:

``````import numpy as np

new_arr = np.arange(12)
result= new_arr
print("Index list number:",result)``````

Here is the execution of the following given code

## Python NumPy index where

• In this section, we will discuss how to get the index value by using numpy.where() function in Python.
• In this example first, we will create an array by using the np.array() function. After that we are going to use the np.where() function and set the condition whose array values is less than 32 those data values are replaced with true. otherwise, it will return false.

Source Code:

``````import numpy as np

new_array = np.array([[55,65,85], [23,32,37]])
result=np.where(new_array<32,True,False)
print(result)``````

Here is the execution of the following given code

## Python NumPy sort with index

• In this Program, we will discuss how to sort the numpy array with index in Python.
• To do this task we are going to use the np.lexsort() function. In Python, this function is used to return a numpy array with indices that sort the keys along with the given axis.
• In this method, the keys parameter must be an iterable sequence of objects that can be converted to NumPy arrays of the same shape.
• Let’s take an example and understand the working of np.lexsort() function
``````import numpy as np

new_array = np.array([16, 75, 8,18,3,1,45])
new_val = np.array([3,4,5,0,1,2,7])
final_output = np.lexsort((new_array, new_val))
print(final_output)``````

In the above code, we imported the numpy library and then initialize an array by using the np.array() function. After that, we use the np.lexsort() function and assign arrays in it.

You can refer to the below Screenshot

## Python NumPy exclude index

• In this Program, we will discuss to exclude index in NumPy array Python.
• By using the np.delete() function we can easily solve this task and return the numpy array that contains only specific values. In Python, the np.delete() function is used to delete the elements from a given array.
• In this program, we have used the index numbers in the list that indicates which element we want to exclude from the array.

Syntax:

Here is the Syntax of numpy.delete() function

``````numpy.delete
(
arr,
obj,
axis=None
)``````

Example:

``````import numpy as np

new_arr = np.array([15, 16, 23, 98, 156, 17, 19, 45, 90])
print("Creation of array:",new_arr)
exc_ind = [4, 0, 3]

result = np.delete(new_arr, exc_ind)
print("After excluding specific index:",result)``````

In the above code, we used the np.delete() function and within this method, we have assigned the array and the index list ‘exc_ind’. Once you will print ‘result’ then the output will display the updated array.

Here is the implementation of the following given code

## Python NumPy return index of max value

• Let us see how to return the index of maximum value in NumPy array Python.
• To perform this particular task we are going to use the np.max() function. In Python, the numpy.max() is used to get the maximum value from a numpy array along with a specified axis and this function is available in the numpy module package.
• In this Program, we have to find the index number of max value. To do this task first we will declare an array and use the max() function that stores the array ‘new_values’. After that declare a variable ‘result’ and use the list comprehension method.

Syntax:

Here is the Syntax of numpy.argmax() function

``````numpy.max
(
axis=None,
out=None,
keepdims=False,
initial=<no value>,
where=True
)``````

Example:

Let’s take an example and check how to return an index of the maximum value in Python

Source Code:

``````import numpy as np

new_values= ([15,67,28,55,3,2,65,43,23,9,76,2])
print("maximum value in array:",(np.max(new_values)))
ret_ind = max(new_values)
result = [i for i in range(0,len(new_values)) if new_values[i] == ret_ind]
print("Index number of maximum value:",result)``````

Here is the output of the following given code

As you can see in the Screenshot the output displays the index number ’10’.

## Python NumPy remove index

• In this section, we will discuss how to remove an index in NumPy array Python.
• To perform this particular task we are going to use the np.delete() function for removing specific elements from the array with a specified index. In Python, the np.delete() function is used for removing elements from a given array.

Syntax:

Here is the Syntax of numpy.delete() function

``````numpy.delete
(
arr,
obj,
axis=None
)``````

Example:

``````import numpy as np

new_values = np.array([56, 89, 167, 345, 987, 156, 345, 178, 256, 706])
new_ind = [4, 2, 1]
new_output = np.delete(new_values, new_ind)
print(new_output)``````

In the above code first, we imported the numpy library and then initialize an array by using the np.array() function. After that, we declared a variable ‘new_ind’ and assign the list of numbers that specifies which element we want to remove from an array. Once you will print ‘new_output’ then the result will display the updated new array.

Here is the implementation of the following given code

## Python NumPy random index

• In this section, we will discuss how to get the index of random in NumPy Python.
• By using random.choice() function, we can perform this particular task. In Python, the random() choice always returns a random value from the specified sequence and the sequence can be array, list, or tuple.
• This method accepts only one argument the list which you want to select an element.
• Let’s have a look at the example and understand the working of random.choice() function.
``````import numpy as np
import random

new_arr = [16,12,76,14,15,11,25,34]
b=random.choice(list(enumerate(new_arr)))
print("index number:",b)``````

Here is the Screenshot of the following given code

## Python NumPy reset index

• In this Program, we will discuss how to reset the index in numpy array Python.
• To perform this particular task we are going to use the np.broadcast() method. In Python np.broadcast() is used to broadcast the input parameters against one another.

Syntax:

Here is the Syntax of numpy.broadcast() method

``class numpy.broadcast()``

Source Code:

``````import numpy as np

new_arr1 = np.array([12, 25, 78])
new_arr2 = np.array([, , ])
print(result.index)
``````

You can refer to the below Screenshot

## Python NumPy unique index

• Here we can see how to get the unique index number from a numpy array Python.
• By using the np.delete() function we can easily solve this task and return the numpy array that contains only specific values.
• In this example, we are going to use the concept of numpy.delete() that will help the user to get the unique elements from an array.

Syntax:

Here is the Syntax of numpy.delete() method

``````numpy.delete
(
arr,
obj,
axis=None
)``````

Example:

``````import numpy as np

values = np.array([15, 16, 16, 98, 156, 17, 156, 45, 90])
print("Initialize array:",values)
uni_ind = [1, 4]

result = np.delete(values, uni_ind)
print("After getting unique index:",result)``````

Here is the implementation of the following given code

## Python NumPy get index of min value

• In this section, we will discuss how to return the index of the minimum value in NumPy array Python.
• To do this task we are going to use the np.min() function. In Python, the numpy.min() is used to get the minimum value from a numpy array along with a specified axis and this function is available in the numpy module package.
• In this Program, we have to find the index number of minimum values. To do this task first we will declare an array and use the min() function that stores the array ‘new_values‘. After that declare a variable ‘result’ and use the list comprehension method.

Source code:

``````import numpy as np

array_elements= ([6,14,76,189,145,876,198,923,2,3,76,1])
print("minimum value in array:",(np.min(array_elements)))
ret_ind = min(array_elements)
result = [i for i in range(0,len(array_elements)) if array_elements[i] == ret_ind]
print("Index number of minimum value:",result)``````

Here is the execution of the following given code

## Python NumPy logical indexing

• In this section, we will discuss how to use the np.logical() function in numpy array Python.
• In Python the np.logical_or() function is used to generate the truth value of arr1 or arr2.

Syntax:

Here is the Syntax of numpy.logical_or() method

``````numpy.logical_or
(
arr1,
arr2,
/,
out=None,
*,
where=True,
casting='same_kind',
order='K',
dtype=None,
subok=true,
)   ``````

Example:

``````import numpy as np

new_arr = np.array([22,34,78,82,94])
result = np.logical_or(new_arr < 22, new_arr > 78)
print(result)``````

Here is the Screenshot of the following given code

As you can see in the Screenshot the output displays the boolean value index.

## Python NumPy boolean indexing

• In this section, we will discuss how to get the index number from the boolean value.
• By using the array condition we can perform this particular task and get the index number. To do this task first we will create an array by using the np.array() function and assign integer values to it.

Example:

``````import numpy as np

new_arr1 = np.array([15, 78, 91])
new_arr2 = np.array([True, False, True])
print("Boolean value index:",new_arr1[new_arr2])``````

Here is the execution of the following given code

As you can see in the Screenshot the output displays the values.

## Python np.ravel_multi_index

• In this section, we will discuss how to use the np.ravel_multi_index() function in numpy array Python.
• In Python, this function is used to convert a tuple of index numpy array within an array of flat indices.

Let’s have a look at the Syntax of np.ravel_multi_index() method

``````numpy.ravel_multi_index
(
multi_index,
dims,
mode='raise',
order='C'
)``````

Source Code:

``````import numpy as np

arr = np.array([[3,6,6],[4,5,1]])
d=np.ravel_multi_index(arr, (7,6))
print(d)``````

Here is the Screenshot of the following given code

In this Python tutorial, we have learned how to get the index number in the NumPy array. Also, we have covered these topics.

• Python NumPy indexing array
• Python numpy index of value
• Python NumPy index of max value
• Python NumPy index of element
• Python NumPy index of minimum
• Python NumPy index condition
• Python NumPy index function
• Python NumPy index list
• Python NumPy index search
• Python NumPy index where
• Python NumPy get index where value is true
• Python NumPy sort with index
• Python NumPy exclude index
• Python NumPy return index of max value
• Python NumPy remove index
• Python NumPy random index
• ‘Python NumPy reset index
• Python NumPy unique index
• Python NumPy get index of min value
• Python NumPy logical indexing
• Python NumPy boolean indexing
• Python np.ravel_multi_index