Do you want to filter a 2D array in Python? In this Python blog, I will tell you different ways **NumPy filter 2D array by condition in Python** using some examples.

**To filter a 2D NumPy array by condition in Python, you can use techniques like boolean indexing for direct element-wise selection, np.where() to locate elements, and combine conditions using logical operators. Additionally, np.all() and np.any() are useful for row-wise or column-wise filtering based on uniform or any-true conditions, respectively. For more complex criteria, np.asarray() with a mean() can convert data structures and apply aggregate condition checks.**

## NumPy Filter 2D Array by Condition in Python

Filtering involves selecting elements from an array that meet certain criteria. Let’s see some common methods to filter a 2D array in NumPy:

- Using Boolean Indexing
- Using np.where
- Combining Conditions
- Using numpy.any() method
- Using numpy.all() method
- Using np.asarray() method

Let’s see them one by one using some illustrative examples:

### 1. NumPy filter 2D array in Python using boolean indexing

The **Boolean indexing** in NumPy allows us to filter an array using a boolean expression. Here’s an example:

```
import numpy as np
stock_prices = np.random.uniform(45, 100, size=(30, 5))
days_with_low_prices = stock_prices[stock_prices < 50]
print("Days with stock prices below $50:\n", days_with_low_prices)
```

**Output:**

```
Days with stock prices below $50:
[46.07200099 49.96345168 47.27621964 48.67303959 49.542139 46.54178479
49.45113595 47.25793938 47.46435936 48.81178511 45.55335432 45.29821599
49.06888685]
```

The output from running the code in PyCharm is visually represented in the screenshot below.

### 2. Filter 2D array Python using np.where() function

The **np.where()** function is a versatile way in NumPy filter 2D array by condition in Python. It returns the indices of elements that satisfy the given condition.

```
import numpy as np
covid_cases = np.random.randint(800, 1500, size=(4, 3))
hotspots = np.where(covid_cases > 1000)
print("Weeks and states with more than 1000 cases:\n", hotspots)
```

**Output:**

```
Weeks and states with more than 1000 cases:
(array([0, 0, 1, 1, 2], dtype=int64), array([0, 1, 1, 2, 0], dtype=int64))
```

Displayed below is a screenshot capturing the outcome of the code execution in the PyCharm editor.

### 3. Python filter 2D array using combining conditions

We can combine **multiple conditions** using logical operators like **& (and) and | (or)** in NumPy filter 2D array by condition in Python.

```
import numpy as np
developer_data = np.random.randint(400, 800, size=(10, 1))
manager_data = np.random.randint(20, 50, size=(10, 1))
combined_data = np.hstack((developer_data, manager_data))
high_ratio_company = combined_data[(combined_data[:, 0] > 500) & (combined_data[:, 1] < 30)]
print("companies with high developer-to-manager ratio:\n", high_ratio_company)
```

**Output:**

```
companies with high developer-to-manager ratio:
[[784 28]
[582 21]
[761 27]]
```

The following screenshot illustrates the results obtained from executing the code in the PyCharm editor.

### 4. NumPy filter array by condition using numpy.any() method

The **np.any()** method tests whether any array element along a given axis evaluates to True. It’s often used in filtering to check if any element in an array or sub-array satisfies a condition in Python.

```
import numpy as np
temperature_data = np.array([
[90, 85, 92, 88],
[95, 102, 99, 101],
[88, 91, 87, 89],
[101, 98, 95, 102]
])
heatwave_weeks = temperature_data[np.any(temperature_data > 100, axis=1)]
print("Heatwave Weeks:\n", heatwave_weeks)
```

**Output:**

```
Heatwave Weeks:
[[ 95 102 99 101]
[101 98 95 102]]
```

After executing the code in Pycharm, one can see the output in the below screenshot.

### 5. NumPy filter using np.all() function

The **np.all() function** in Python checks if all elements along a specified axis satisfy a condition. It is useful for filtering out complete rows or columns based on a uniform condition.

```
import numpy as np
aqi_data = np.array([
[45, 40, 50],
[55, 60, 52],
[42, 38, 47],
[30, 28, 35]
])
good_air_quality_days = aqi_data[np.all(aqi_data < 50, axis=1)]
print("Good Air Quality Days:\n", good_air_quality_days)
```

**Output:**

```
Good Air Quality Days:
[[42 38 47]
[30 28 35]]
```

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

### 6. Filter 2D NumPy array using np.asarray() function

The **np.asarray() function** in Python converts an input to an array. While it’s not a filtering function per se, it can be part of a filtering process, especially when dealing with data that might not initially be in array form.

```
import numpy as np
Match_scores_list = [
[1050, 1100, 1020],
[1250, 1300, 1280],
[1150, 1200, 1180],
]
Match_scores = np.asarray(Match_scores_list)
high_scoring_company = Match_scores[np.mean(Match_scores, axis=1) > 1200]
print("High Scoring company:\n", high_scoring_company)
```

**Output:**

```
High Scoring company:
[[1250 1300 1280]]
```

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

## Conclusion

Learning a variety of methods like **boolean indexing**, **conditional operators**, **np.where()**, **np.all()**, **np.any()**, and **np.asarray() **with **mean() functions** in **NumPy filter 2D array by condition in Python**. These functions are helpful for sorting through data, allowing us to pick out exactly what we need based on specific conditions.

You may also like to read:

- How to create an empty matrix in Python
- Python repeat array n times
- NumPy normalize 0 and 1 in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etcâ€¦ for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.