Advanced Indexing
Learn how to filter and manipulate arrays using boolean conditions and array-based indexing.
Learn how to filter and manipulate arrays using boolean conditions and array-based indexing. This hands-on tutorial focuses on practical implementation of advanced indexing concepts.
Module 6: Advanced Indexing
Advanced indexing allows you to select non-contiguous elements and filter data based on logical conditions. This is the bedrock of data cleaning and filtering in Python.
Lesson 13: Boolean Indexing
Boolean indexing uses a "mask" (an array of True/False values) to pick elements.
How it works:
- You apply a condition to an array (e.g.,
arr > 5). - This creates a boolean mask of the same shape.
- You pass that mask back into the array to get only the
Truevalues.
Multiple Conditions:
Use & (and), | (or), and ~ (not) for bitwise logic. Note: Parentheses are mandatory when combining conditions.
Lesson 14: Fancy Indexing
Fancy indexing refers to passing an array of indices to access multiple elements at once.
Selection with Lists
You can pass a list of specific indices you want to extract.
arr[[0, 2, 5]] extracts elements at 0, 2, and 5.
MD Selection
In 2D arrays, you can pass lists for both rows and columns.
matrix[[0, 1], [2, 0]] picks (0,2) and (1,0).
Mini Project: Filter Large CSV Data
Imagine you have a dataset of 1000 items with random prices and categories.
Challenge: Create an array of 100 random prices (np.random.randint(10, 500, 100)). Filter the prices that are between 100 and 200 using boolean indexing.
Quiz
Question 1 of 5Which operator is used for the 'logical AND' in boolean indexing?
Key Takeaways
✅ Boolean indexing is the fastest way to filter data.
✅ Always use parentheses when using multiple boolean conditions.
✅ Fancy indexing allows picking specific, non-contiguous elements.