---
title: "How to filter an array using NUmpy?"  
description: "How to filter an array using NUmpy?"  
author: "Ravi Vishwakarma"  
published: 2025-11-23  
updated: 2025-11-23  
canonical: https://www.mindstick.com/interview/34416/how-to-filter-an-array-using-numpy  
category: "python"  
tags: ["python-3.4", "numpy"]  
reading_time: 4 minutes  

---

# How to filter an array using NUmpy?

To **filter (select) values from a** [**NumPy array**](https://www.mindstick.com/interview/34411/how-to-iterating-numpy-array), you use **boolean indexing** — create a boolean condition and apply it to the array.

## Basic Example

```python
import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])

filtered = arr[arr > 3]
print(filtered)
```

## Output

```plaintext
[4 5 6]
```

## Filter Using Multiple Conditions

Use `&` (AND), `|` (OR), and `~` (NOT).\
Parentheses are required.

```python
arr = np.array([10, 20, 30, 40, 50])

filtered = arr[(arr >= 20) & (arr <= 40)]
print(filtered)
```

Output:

```plaintext
[20 30 40]
```

## Filter with Another Array of Booleans

```python
arr = np.array([5, 10, 15, 20])
mask = np.array([True, False, True, False])

filtered = arr[mask]
print(filtered)
```

Output:

```plaintext
[ 5 15 ]
```

## Filter Strings

```python
names = np.array(["Bob", "Alice", "John", "Anna"])
filtered = names[names == "Anna"]
print(filtered)
```

To [filter strings](https://www.mindstick.com/interview/34414/how-to-search-in-numpy-arrays) containing a substring:

```python
filtered = names[np.char.find(names, "n") >= 0]   # find returns -1 if not found
print(filtered)
```

## Filter 2D Arrays

```python
arr = np.array([[1, 2, 3],
                [4, 5, 6],
                [7, 8, 9]])

filtered = arr[arr > 5]
print(filtered)
```

Output:

```plaintext
[6 7 8 9]
```

## Replace filtered values instead of selecting

```python
arr = np.array([10, 20, 30, 40])
arr[arr > 25] = 0
print(arr)
```

Output:

```plaintext
[10 20  0  0]
```

## Get indexes of matches

```python
arr = np.array([3, 7, 2, 9, 7])
idx = np.where(arr == 7)
print(idx)
```

Result:

```plaintext
(array([1, 4], dtype=int64),)
```

### Summary

| Goal | Code |
| --- | --- |
| Select values | `arr[arr > 3]` |
| Select with conditions | `arr[(arr >= 10) & (arr <= 20)]` |
| Boolean mask | `arr[mask]` |
| Replace selected | `arr[arr > 10] = 0` |
| Get indexes | `np.where(arr == x)` |

## Answers

### Answer by Ravi Vishwakarma

To **filter (select) values from a** [**NumPy array**](https://www.mindstick.com/interview/34411/how-to-iterating-numpy-array), you use **boolean indexing** — create a boolean condition and apply it to the array.

## Basic Example

```python
import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])

filtered = arr[arr > 3]
print(filtered)
```

## Output

```plaintext
[4 5 6]
```

## Filter Using Multiple Conditions

Use `&` (AND), `|` (OR), and `~` (NOT).\
Parentheses are required.

```python
arr = np.array([10, 20, 30, 40, 50])

filtered = arr[(arr >= 20) & (arr <= 40)]
print(filtered)
```

Output:

```plaintext
[20 30 40]
```

## Filter with Another Array of Booleans

```python
arr = np.array([5, 10, 15, 20])
mask = np.array([True, False, True, False])

filtered = arr[mask]
print(filtered)
```

Output:

```plaintext
[ 5 15 ]
```

## Filter Strings

```python
names = np.array(["Bob", "Alice", "John", "Anna"])
filtered = names[names == "Anna"]
print(filtered)
```

To [filter strings](https://www.mindstick.com/interview/34414/how-to-search-in-numpy-arrays) containing a substring:

```python
filtered = names[np.char.find(names, "n") >= 0]   # find returns -1 if not found
print(filtered)
```

## Filter 2D Arrays

```python
arr = np.array([[1, 2, 3],
                [4, 5, 6],
                [7, 8, 9]])

filtered = arr[arr > 5]
print(filtered)
```

Output:

```plaintext
[6 7 8 9]
```

## Replace filtered values instead of selecting

```python
arr = np.array([10, 20, 30, 40])
arr[arr > 25] = 0
print(arr)
```

Output:

```plaintext
[10 20  0  0]
```

## Get indexes of matches

```python
arr = np.array([3, 7, 2, 9, 7])
idx = np.where(arr == 7)
print(idx)
```

Result:

```plaintext
(array([1, 4], dtype=int64),)
```

### Summary

| Goal | Code |
| --- | --- |
| Select values | `arr[arr > 3]` |
| Select with conditions | `arr[(arr >= 10) & (arr <= 20)]` |
| Boolean mask | `arr[mask]` |
| Replace selected | `arr[arr > 10] = 0` |
| Get indexes | `np.where(arr == x)` |


---

Original Source: https://www.mindstick.com/interview/34416/how-to-filter-an-array-using-numpy

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
