The NumPy argwhere() method finds indices of array elements that are not zero as a 2D array.
argwhere() Syntax
The syntax of argwhere() is:
numpy.argwhere(array)
argwhere() Argument
The argwhere() method takes one argument:
array- an array whose non-zero indices are to be found
argwhere() Return Value
The argwhere() method returns indices of elements that are non-zero as a 2D array.
Example 1: numpy.argwhere() With Arrays
Output
Array of non-empty indices in numberArray: [[0] [3] [4]] Array of non-empty indices in stringArray: [[0] [1] [3]]
Example 2: numpy.argwhere() With 2-D Arrays
Output
[[0 0] [0 2] [1 0] [2 1] [2 2]]
Here, the output represents the positions of non-zero elements in the row-column format.
The first non-zero element is 1, which is in index [0, 0] in row-column format. Similarly, the second non-zero element is 3, which is in index [0, 2] in row-column format, and so on.
Example 3: numpy.argwhere() With Condition
We can also use argwhere() to find the indices of elements that satisfy the given condition.
Output
[[1] [3] [5]]
Note: To group the indices by the dimension, rather than element, we use nonzero().