How to Use Boolean Indexing in NumPy for Easy Data Selection
In NumPy,
boolean indexing lets you select elements from an array by using a boolean array of the same shape, where True means keep the element and False means discard it. You create a boolean condition on the array, then use it inside square brackets to get the filtered result.Syntax
Boolean indexing uses a boolean array or condition inside square brackets to select elements from a NumPy array.
Syntax parts:
array: Your original NumPy array.condition: A boolean array or expression that returnsTrueorFalsefor each element.array[condition]: Returns a new array with elements where the condition isTrue.
python
filtered_array = array[condition]
Example
This example shows how to select all numbers greater than 5 from a NumPy array using boolean indexing.
python
import numpy as np array = np.array([1, 3, 7, 9, 2, 6]) condition = array > 5 filtered_array = array[condition] print(filtered_array)
Output
[7 9 6]
Common Pitfalls
Common mistakes when using boolean indexing include:
- Using a boolean array of a different shape than the original array, which causes an error.
- Forgetting that the condition must be a boolean array, not just a number or string.
- Trying to assign values to a filtered array without using the original array, which does not change the original data.
python
import numpy as np array = np.array([1, 2, 3, 4]) # Wrong: boolean array shape mismatch try: wrong_condition = np.array([True, False]) print(array[wrong_condition]) except Exception as e: print(f"Error: {e}") # Right: boolean array matches shape right_condition = array % 2 == 0 print(array[right_condition])
Output
Error: boolean index did not match indexed array along dimension 0; dimension is 4 but corresponding boolean dimension is 2
[2 4]
Quick Reference
| Operation | Example | Description |
|---|---|---|
| Select elements > 5 | array[array > 5] | Returns elements greater than 5 |
| Select even numbers | array[array % 2 == 0] | Returns even elements |
| Select elements in range | array[(array > 2) & (array < 8)] | Returns elements between 2 and 8 |
| Assign to filtered elements | array[array < 3] = 0 | Sets elements less than 3 to zero |
Key Takeaways
Boolean indexing uses a boolean array to select elements from a NumPy array.
The boolean array must have the same shape as the original array.
You can combine conditions with & (and), | (or) for complex filtering.
Boolean indexing returns a new array with only the selected elements.
Assigning to boolean indexed elements changes the original array.