numpy.nonzero() in Python Last Updated : 30 Jun, 2025 Comments Improve Suggest changes Like Article Like Report numpy.nonzero() function returns the indices of the elements in an array that are non-zero. It is commonly used to find the positions of non-zero (or True) elements in arrays.Example: Python import numpy as np a = np.array([0, 2, 0, 3, 0, 4]) res = np.nonzero(a) print(res) Output(array([1, 3, 5]),) Explanation: The elements at index 1, 3 and 5 are non-zero, so np.nonzero() returns their indices.Syntaxnumpy.nonzero(a)Parameters: a is Input array (can be 1D, 2D or higher dimensional).Returns: This method returns a tuple of arrays, one for each dimension of the input array. Each array contains the indices of the non-zero elements along that dimension.ExamplesExample 1: 2D array Python import numpy as np a = np.array([[0, 1, 0], [2, 0, 3]]) res = np.nonzero(a) print(res) Output(array([0, 1, 1]), array([1, 0, 2])) Explanation: np.nonzero(a) returns a tuple of two arrays: the first array represents the row indices and the second represents the column indices where the non-zero elements are located.Example 2: Using with array indexing Python import numpy as np a = np.array([[0, 1], [2, 0]]) rows, cols = np.nonzero(a) print(rows) print(cols) Output[0 1] [1 0] Explanation: We unpack np.nonzero(a) into rows and cols, which represent the row and column indices of non-zero elements. Here, a[0, 1] = 1 and a[1, 0] = 2, so the output [0 1] and [1 0] gives their positions. Comment More infoAdvertise with us Next Article numpy.nonzero() in Python J jana_sayantan Follow Improve Article Tags : Python Python-numpy Python numpy-Indexing Practice Tags : python Similar Reads numpy.isnan() in Python The numpy.isnan() function tests element-wise whether it is NaN or not and returns the result as a boolean array. Syntax : numpy.isnan(array [, out]) Parameters : array : [array_like]Input array or object whose elements, we need to test for infinity out : [ndarray, optional]Output array placed wit 2 min read numpy.flatnonzero() in Python numpy.flatnonzero()function is used to Compute indices that are non-zero in the flattened version of arr. Syntax : numpy.flatnonzero(arr) Parameters : arr : [array_like] Input array. Return : ndarray Output array, containing the indices of the elements of arr.ravel() that are non-zero. Code #1 : Wor 1 min read numpy.nansum() in Python numpy.nansum() function computes the sum of array elements over a given axis, treating NaN (Not a Number) values as zero. This is useful when you want to ignore missing or undefined values in your computation. For Example:Pythonimport numpy as np a = np.array([1.0, 2.0, np.nan, 4.0]) res = np.nansum 2 min read Numpy count_nonzero method | Python numpy.count_nonzero() function counts the number of non-zero values in the array arr. Syntax : numpy.count_nonzero(arr, axis=None) Parameters : arr : [array_like] The array for which to count non-zeros. axis : [int or tuple, optional] Axis or tuple of axes along which to count non-zeros. Default is 1 min read numpy.isneginf() in Python The numpy.isneginf() function tests element-wise whether it is negative infinity or not, and returns the result as a boolean array. Syntax :  numpy.isneginf(array, y = None) Parameters : array : [array_like]Input array or object whose elements, we need to test for infinity. y : [array_like]A boole 2 min read Like