Lesson 5 ❮ Lesson List ❮ Top Page
5.4 Join, Split, and Transpose
❯ 5.5 Search and Filter
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
EXPECTED COMPLETION TIME
❲▹❳ Video 7m 3s
☷ Interactive readings 5m
✑ Practice 5.5 (G Colab) 25m
Getting some elements out of an existing array and creating a new array out of them is called filtering.
In NumPy, you filter an array using a boolean index list An element is contained in the filtered array only if the value at an index is True.
Use bitwise operator &, |, ~ for and, or, and not, respectively.
Using a list comprehension with if statement can be useful for filtering. While there is no ndarray comprehension, a similar task can be done using this method:
where(condition, x, y)
returns elements chosen from x or y depending on condition.
Unlike list, NumPy can only work with 1 data type. We can force NumPy to work with multiple data types by assigning a dtype. The resulting array is called a structured array.
To sort ndarray, you can use:
np.sort(arr)
returns a sorted copy of an array.