# How To Use NumPy Boolean Indexing To Filter And Manage Data Array

Boolean indexing in NumPy is a powerful technique that allows you to efficiently filter and manipulate arrays based on certain conditions. By using Boolean arrays, you can select subsets of data that meet specific criteria, perform operations on them, and even modify the original data array. In this guide, we’ll delve into the intricacies of Boolean indexing in Python using NumPy, exploring various examples and practical applications.

## 1. Example Data.

Let’s start by defining some example data for illustration purposes:

```import numpy as np

# Example array of names with duplicates
names = np.array(["Alice", "Bob", "Charlie", "Alice", "David", "Bob", "Charlie"])

# Example 2D array of corresponding data
data = np.array([[10, 25], [5, 12], [8, 20], [3, 9], [15, 30], [7, 18], [12, 24]])

print("Names:", names)
print("Data:")
print(data)```

Output.

```Names: ['Alice' 'Bob' 'Charlie' 'Alice' 'David' 'Bob' 'Charlie']
Data:
[[10 25]
[ 5 12]
[ 8 20]
[ 3  9]
[15 30]
[ 7 18]
[12 24]]```

## 2. Filtering Data with Boolean Arrays.

### 2.1 Selecting Rows Based on Conditions.

Suppose we want to select all rows corresponding to the name “Alice“. We can achieve this using Boolean indexing:

```alice_data = data[names == "Alice"]
print("Data for Alice:")
print(alice_data)```

Output.

```Data for Alice:
[[10 25]
[ 3  9]]```

Why can the code generate the above output.

```In [4]: index_arr = names == "Alice"

In [5]: index_arr
Out[5]: array([ True, False, False,  True, False, False, False])

In [6]: data[index_arr]
Out[6]:
array([[10, 25],
[ 3,  9]])```

### 2.2 Combining Conditions: Using Boolean Arithmetic Operators.

We can combine multiple conditions to filter data efficiently. Let’s say we want data for “Alice” or “Bob“:

```alice_or_bob_data = data[(names == "Alice") | (names == "Bob")]
print("Data for Alice or Bob:")
print(alice_or_bob_data)```

Output.

```Data for Alice or Bob:
[[10 25]
[ 5 12]
[ 3  9]
[ 7 18]]```

Explain.

```In [8]: index_arr1 = (names == "Alice") | (names == "Bob")

In [9]: index_arr1
Out[9]: array([ True,  True, False,  True, False,  True, False])

In [10]: data[index_arr1]
Out[10]:
array([[10, 25],
[ 5, 12],
[ 3,  9],
[ 7, 18]])```

## 3. Modifying Data.

### 3.1 Updating Values Based on Conditions.

Boolean indexing also allows us to modify data easily. For instance, let’s replace all the number values which are less than 10 in the data with zeros:

```data = np.array([[10, 25], [5, 12], [8, 20], [3, 9], [15, 30], [7, 18], [12, 24]])
print("Original data")
print(data)
print("\r\n")
data[data < 10] = 0
print("Data after replacing number value which less than 10 with zeros:")
print(data)```

Output.

```Original data
[[10 25]
[ 5 12]
[ 8 20]
[ 3  9]
[15 30]
[ 7 18]
[12 24]]

Data after replacing number value which less than 10 with zeros:
[[10 25]
[ 0 12]
[ 0 20]
[ 0  0]
[15 30]
[ 0 18]
[12 24]]```

Explain.

```In [17]: index_arr1 = data < 10

In [18]: index_arr1
Out[18]:
array([[False, False],
[ True, False],
[ True, False],
[ True,  True],
[False, False],
[ True, False],
[False, False]])```

### 4.1 Setting Rows or Columns with Boolean Arrays.

We can set entire rows or columns based on Boolean conditions.

For example, let’s set all rows where the name is not “Charlie” to a specific value:

```names = np.array(["Alice", "Bob", "Charlie", "Alice", "David", "Bob", "Charlie"])
print("Names:", names)

data = np.array([[10, 25], [5, 12], [8, 20], [3, 9], [15, 30], [7, 18], [12, 24]])
print("Original data")
print(data)

data[names != "Charlie"] = 999
print("Data after setting rows not corresponding to 'Charlie' to 999:")
print(data)```

Output.

```Names: ['Alice' 'Bob' 'Charlie' 'Alice' 'David' 'Bob' 'Charlie']
Original data
[[10 25]
[ 5 12]
[ 8 20]
[ 3  9]
[15 30]
[ 7 18]
[12 24]]
Data after setting rows not corresponding to 'Charlie' to 999:
[[999 999]
[999 999]
[  8  20]
[999 999]
[999 999]
[999 999]
[ 12  24]]```

Explain.

```In [20]: names != "Charlie"
Out[20]: array([ True,  True, False,  True,  True,  True, False])

In [21]: index_arr1 = names != "Charlie"

In [22]: index_arr1
Out[22]: array([ True,  True, False,  True,  True,  True, False])```

## 5. Conclusion.

Boolean indexing in NumPy provides a flexible and efficient way to manipulate arrays based on conditions. By mastering this technique, you can streamline data filtering and modification tasks in Python, making your code more concise and readable. Experiment with different examples and explore the full potential of Boolean indexing in your data analysis and manipulation workflows.