# How to filter NumPy array by two conditions

In this post, we are going to learn How to filter the NumPy array by two conditions.We are going to use NumPy built-in function logical_and(), np.greater(),np.less() ,logical_not(),logical_or() or in some example we have used Numpy logical AND and OR operator.To use a NumPy library first we have to install it on the local machine and Import it into our program using import NumPy as np

### 1. How to filter NumPy array by two conditions using logical_and()

In this python program first, we have filtered the Numpy array using logical_and() function and passed np. greater(myarr, 10) and np. less(myarr, 25) as arguments to filter the NumPy array elements which are greater than 10 and less than 25 that will return a mask array. We have used mask in array indexing to filter the array based on two conditions. This is How to filter the NumPy array by two conditions using the logical_and() function.

```import numpy as np

myarr = np.arange(25).reshape((5, 5))

print(myarr)

resultarr = np.logical_and(np.greater(myarr, 10), np.less(myarr, 25))
print('FIlter array:',myarr[resultarr])
```

Output

```[[ 0  1  2  3  4]
[ 5  6  7  8  9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
[11 12 13 14 15 16 17 18 19 20 21 22 23 24]
```

### 2. How to filter NumPy array by two conditions using logical_not

In this Python program, we have to filter the NumPy array using logical_not() function that will return a mask array and used this mask array. We have used mask in array indexing to filter the array based on two conditions.

```import numpy as np

myarr = np.arange(25).reshape((5, 5))

print(myarr)

resultarr = np.logical_not(myarr>7, myarr < 18)

print('Filter array:\n',myarr[resultarr])

```

Output

```[[ 0  1  2  3  4]
[ 5  6  7  8  9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
Filter array:
[0 1 2 3 4 5 6 7]
```

### 3. How to filter NumPy array by two conditions using logical_or

In this Python program example, we have to filter the NumPy array by passing two condition with the help of logical_or() function that returns Mask array .We have passed masked array to array indexing to get a filtered array.

```import numpy as np

myarr = np.arange(25).reshape((5, 5))

print(myarr)

resultarr = np.logical_or(myarr>7, myarr==18)

print('Filter array:\n',myarr[resultarr])

```

Output

```[[ 0  1  2  3  4]
[ 5  6  7  8  9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
Filter array:
[ 8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24]
```

### 4. Bitwise or operator to filter NumPy array by two condition

In this Python program example,We have used bitwise OR operator to file to get the mask array of boolean values and used the array indexing to filter array based on two condition.

```import numpy as np

myarr = np.arange(25).reshape((5, 5))

print(myarr)

resultarr  = (myarr < 3) | (myarr == 6)

print('Filter array:\n',myarr[resultarr])
```

Output

```[[ 0  1  2  3  4]
[ 5  6  7  8  9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
Filter array:
[0 1 2 6]
```

### 5. Bitwise AND operator to filter NumPy array by two condition

In this Python program, We have used bitwise AND operator to get merged mask array of boolean values for both conditions and used the array indexing to filter array based on two condition.

```import numpy as np

myarr = np.arange(25).reshape((5, 5))

print(myarr)

resultarr  = (myarr < 10) & (myarr == 6)

print('Filter array:\n',myarr[resultarr])

```

Output

```[[ 0  1  2  3  4]
[ 5  6  7  8  9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
Filter array:

```

### Summary

In this post we have learned mutiple ways of how to filter NumPy array by two conditions.We have used the NumPy built-in function logical_and(), np.greater(),np.less() ,logical_not(),logical_or() with examples