JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Find the first index of an element in a NumPy array

Author:JIYIK Last Updated:2025/05/03 Views:

In this tutorial, we will discuss how to find the first index of an element in a numpy array.


Use where()the function to find the first index of an element in a NumPy array

The function in the numpy module where()is used to return an array containing the indices of the elements that satisfy some condition. The condition is specified in the function.

We can use this to find the first index of a specific value in an array as shown below.

a = np.array([7, 8, 9, 5, 2, 1, 5, 6, 1])

print(np.where(a == 1)[0][0])

Output:

5

Use nonzero()the function to find the first index of an element in a NumPy array

nonzero()The function returns the indices of all non-zero elements in a numpy array. It returns a tuple of arrays for multidimensional arrays.

Similar to where()the function, we can also specify the condition so that it can also return the position of a specific element.

For example,

a = np.array([7, 8, 9, 5, 2, 1, 5, 6, 1])

print(np.nonzero(a == 1)[0][0])

Output:

5

For most basic purposes, the where()and nonzero()functions look very similar. where()The difference between the functions is when you want to select elements from an array awhere some condition holds, and when you want to select elements from an array where that condition holds .TruebFalse


Use argmax()the function to find the first index of an element in a numpy array

argmax()Find the index of the maximum element in an array. We can specify the equality condition in the function and find the index of the required element.

For example,

a = np.array([7, 8, 9, 5, 2, 1, 5, 6, 1])

print(np.argmax(a == 1))

Output:

5

Use index()the function to find the first index of an element in a NumPy array

In this method, we will first tolist()convert the array into a list using the function. Then, we will use index()the function which will return the position of the specified element.

The following code achieves this.

a = np.array([7, 8, 9, 5, 2, 1, 5, 6, 1])

print(a.tolist().index(1))

Output:

5

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Convert Tensor to NumPy array in Python

Publish Date:2025/05/03 Views:85 Category:Python

This tutorial will show you how to convert a Tensor to a NumPy array in Python. Use the function in Python Tensor.numpy() to convert a tensor to a NumPy array Eager Execution of TensorFlow library can be used to convert tensor to NumPy arra

Saving NumPy arrays as images in Python

Publish Date:2025/05/03 Views:193 Category:Python

In Python, numpy module is used to manipulate arrays. There are many modules available in Python that allow us to read and store images. An image can be thought of as an array of different pixels stored at specific locations with correspond

Transposing a 1D array in NumPy

Publish Date:2025/05/03 Views:98 Category:Python

Arrays and matrices form the core of this Python library. The transpose of these arrays and matrices plays a vital role in certain topics such as machine learning. In NumPy, it is easy to calculate the transpose of an array or a matrix. Tra

Converting a PIL image to a NumPy array

Publish Date:2025/05/03 Views:148 Category:Python

This tutorial will discuss methods to convert a PIL image to a 3-dimensional NumPy array in Python. numpy.array() Convert a PIL image to a NumPy array in Python using the PIL is used to perform various operations on images in Python. The Pi

How to Convert DataFrame Column to String in Pandas

Publish Date:2025/05/02 Views:162 Category:Python

We will look at methods for converting Pandas DataFrame columns to strings. Pandas Series.astype(str) Method DataFrame.apply() Methods operate on the elements in a column We will use the same DataFrame below in this article. import pandas a

How to count the frequency of values in a Pandas DataFrame

Publish Date:2025/05/02 Views:84 Category:Python

Sometimes, when you use DataFrame , you may want to count the number of times a value occurs in a column, or in other words, calculate the frequency. There are mainly three methods used for this. Let's look at them one by one. df.groupby().

How to get value from Pandas DataFrame cell

Publish Date:2025/05/02 Views:148 Category:Python

We'll look at using to get values ​​from cells in iloc Pandas , which is great for selecting by position, and how it differs from . We'll also learn about the and methods, which we can use when we don't want to set the return type to .

How to Add a Row to a Pandas DataFrame

Publish Date:2025/05/02 Views:127 Category:Python

Pandas is designed to load a fully populated DataFrame . We can pandas.DataFrame add them one by one in . This can be done by using various methods, such as .loc , dictionary, pandas.concat() or DataFrame.append() . .loc [index] Add rows to

How to change the order of Panas DataFrame columns

Publish Date:2025/05/02 Views:185 Category:Python

We will show how to use insert and reindex to change the order of columns in different ways pandas.DataFrame , such as assigning column names in a desired order. pandas.DataFrame Sort the columns in the new order The easiest way is columns

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial