Using where(), nonzero(), and argwhere() in NumPy

Introduction

In data analysis, it's often not enough to just *look* at data—we need to pinpoint exactly where certain values occur or extract them based on conditions. This is where NumPy’s powerful functions like where(), nonzero(), and argwhere() come into play.

These tools help us interrogate arrays in smart, efficient ways, making them essential for anyone serious about numerical computing.

1. NumPy where() — Conditional Element Selection

numpy.where() acts like a vectorized if-else. It returns indices (or values) based on a condition. Think of it as: "Where in this array does this condition hold true?"

Syntax

numpy.where(condition[, x, y])
  • If only the condition is passed, it returns the indices where it is True.
  • If x and y are given, it returns x where condition is True, and y where False.

Example 1: Get indices where values are positive

import numpy as np

arr = np.array([-3, 0, 2, -1, 5])
result = np.where(arr > 0)
print(result)

Output

(array([2, 4]),)

Explanation

The values at index 2 and 4 are greater than 0. So where() gives us (array([2, 4]),).

Example 2: Apply vectorized conditional logic

result = np.where(arr > 0, "Positive", "Not Positive")
print(result)

Output

['Not Positive' 'Not Positive' 'Positive' 'Not Positive' 'Positive']

Explanation

This time, instead of just finding indices, we used where() to return a new array with custom labels. Super handy for labeling or replacing values conditionally!

2. NumPy nonzero() — Find All Non-Zero Values

nonzero() finds indices of all non-zero elements in an array. It's particularly useful when you're dealing with sparse matrices or binary masks.

Example

arr = np.array([0, 4, 0, 2, 0, 5])
indices = np.nonzero(arr)
print(indices)

Output

(array([1, 3, 5]),)

Explanation

Only positions 1, 3, and 5 contain non-zero values. The result is a tuple containing an array of these indices.

Verification Tip

To double-check what values are at those positions:

print(arr[indices])
[4 2 5]

3. NumPy argwhere() — Like where(), But in Matrix Form

argwhere() is similar to where() but returns the coordinates as rows for multi-dimensional arrays. Each row corresponds to the index of an element meeting the condition.

Example: Locate all values greater than 10

matrix = np.array([[5, 12], [7, 15]])
positions = np.argwhere(matrix > 10)
print(positions)

Output

[[0 1]
[1 1]]

Explanation

This tells us that elements at positions [0,1] and [1,1] (i.e., 12 and 15) are greater than 10.

What to Watch Out For

  • Tuple output: Both where() and nonzero() return tuples. This can confuse beginners expecting a flat array.
  • Dimensionality: argwhere() is best for 2D or ND arrays. For 1D arrays, where() or nonzero() may feel more intuitive.
  • Don’t mix up behavior: where(condition) returns indices, but where(condition, x, y) returns values.

Summary

Each of these functions is a lens through which you can “see” your array in different ways:

  • where(): Conditional filter — indices or values
  • nonzero(): Non-zero detector
  • argwhere(): Position locator in N dimensions

Practice Tip

Try combining these with boolean masks to extract rows, filter data for plots, or segment arrays in preprocessing pipelines. These are not just syntactic tricks — they’re your building blocks for smart, clean, and expressive code.