# Numpy where() with multiple conditions

In this article, we are going to learn Numpy where() with multiple conditions, some of the use cases where we need to filter some data based on multiple conditions to evaluate on some data set. so let us begin with our article.

### 1. numpy.where() with multiple condition Using OR Operator

We are going to take our first example with multiple condition evaluations. In this example, we will evaluate if either condition is true. When we want to allow some operation based on either of two conditions, we use OR logical operation. Here we will use the same logical OR operator in the where() function. let us jump to our example.

```#Program numpy.where() with multiple condition with OR Operator

import numpy as np

# Create an array
arr1 = np.arange(0,10)
arr2 = np.arange(10,20)

print("The values of array 1 :\n", arr1)
print("\nThe values of array 2 :\n", arr2)

result_arr = np.where(((arr1 % 2 == 0) | (arr2 % 2 == 1)),
arr1, arr2)

print("\nThe filtered array is :\n", result_arr)

```

Output

```The values of array 1 :
[0 1 2 3 4 5 6 7 8 9]

The values of array 2 :
[10 11 12 13 14 15 16 17 18 19]

The filtered array is :
[0 1 2 3 4 5 6 7 8 9]
```

### 2. numpy.where() with multiple condition Using AND Operator

In this example, we will evaluate if both of the conditions are true. When we want to allow some operation based on two conditions then we make use of AND logical operation. Here we will use the same logical AND operator in where() function.

```import numpy as np

# Create an array
arr1 = np.arange(0,10)
arr2 = np.arange(10,20)

print("The values of array 1 :\n", arr1)
print("The values of array 2 :\n", arr2)

result_arr = np.where(((arr1 % 2 == 0) & (arr2 % 2 == 1)),
arr1, arr2)

print("The filtered array is :\n", result_arr)
```

Output

```The values of array 1 :
[0 1 2 3 4 5 6 7 8 9]

The values of array 2 :
[10 11 12 13 14 15 16 17 18 19]

The filtered array is :
[10 11 12 13 14 15 16 17 18 19]
```

### 3. NumPy where() with multiple conditions in 2-D arrays

In this example, we are going to apply the numpy.where() function on the 2-D array and we will use multiple conditions in this example. By using multiple conditions, we will filter the array and get the desired results.

```
import numpy as np

# Create two multidimensional arrays of
# integer values
arr1 = np.array([[6, 13, 22, 7, 12],
[7, 11, 16, 32, 9]])
arr2 = np.array([[44, 20, 8, 35, 10],
[98, 23, 42, 6, 13]])

# Print the array values
print("\nThe values of the first array :\n", arr1)
print("\nThe values of the second array :\n", arr2)

#using Logical OR operator on 2-D array
result_arr1 = np.where(((arr1 % 2 == 0) | (arr2 % 2 == 1)),
arr1, arr2)

#using Logical AND operator on 2-D array
result_arr2 = np.where(((arr1 % 2 == 0) & (arr2 % 2 == 1)),
arr1, arr2)

# Print the new array
print("\nThe filtered values of both arrays with OR Condition:\n", result_arr1)
print("\nThe filtered values of both arrays with AND condition :\n", result_arr2)
```

Output

```
The values of the first array :
[[ 6 13 22  7 12]
[ 7 11 16 32  9]]

The values of the second array :
[[44 20  8 35 10]
[98 23 42  6 13]]

The filtered values of both arrays with OR Condition:
[[ 6 20 22  7 12]
[98 11 16 32  9]]

The filtered values of both arrays with AND condition :
[[44 20  8 35 10]
[98 23 42  6 13]]
```

If you see the output, the output is the tuple of arrays.

This contains two arrays. These two arrays contain the index of each dimension. The first array contains the row index and the second array contains the column index.so if you want to know the element position, you will need to take the first value from the first tuple and the second value from the second tuple.

The row indexes will be part of result_arr1 and the column index will be part of result_arr1. We can use the zip() function of Python to make the index positions in a 2-D array.

```Indexpositions1= list(zip(result_arr1, result_arr1))
print(Indexpositions1)
print("\nThe filtered index positions with OR Condition:\n", Indexpositions1)
```

Output

```
The filtered index positions with OR Condition:
[(6, 98), (20, 11), (22, 16), (7, 32), (12, 9)]
```

Similarly, The row indexes will be part of result_arr2 and the column index will be part of result_arr2. We can use Python’s zip() function to make the index positions in a 2-D array.

```Indexpositions2= list(zip(result_arr2, result_arr2))
print(Indexpositions2)
print("\nThe filtered index positions with AND Condition:\n", Indexpositions2)
```

Output

```The filtered index positions with AND Condition:
[(44, 98), (20, 23), (8, 42), (35, 6), (10, 13)]
```

#### Conclusion

In this article, we learned, how we can use numpy.where() function by using multiple conditions. Then by using the multiple conditions in numpy.where() function how we can filter elements in 1-D and 2-D arrays. We hope you learn many useful techniques in this article that will help you in your application code.