Hello, Data Explorer! Today's lesson will focus on "selecting data" in NumPy arrays—our goal is to master integer and Boolean indexing and slicing. So, let's jump in!
In this section, we'll discuss "Indexing". It's akin to item numbering in lists, and Python implements it with a zero-based index system. Let's see this principle in action.
Essentially, indexing is a numeric system for items in an array—relatively straightforward!
Are you mindful of higher dimensions? NumPy arrays can range from 1D to N-dimensions. To access specific elements, we use the index pair (i,j) for a 2D array, (i,j,k) for a 3D array, and so forth.
In this example, we selected the first element of the second row in a 2D-array—it's pretty simple!
Are you ready for some magic? Enter "Boolean Indexing", which functions like a 'Yes/No' filter, with 'Yes' representing True and 'No' for False.
Or we can put the condition directly into [] brackets:
Voila! Now, we can filter data based on custom conditions.
Now that we've mastered the simple 'Yes/No' binary filter system, let's up the ante with "Complex Conditions in Boolean Indexing". This method refines our filtering process further, allowing us to set more detailed restrictions.
Imagine, for instance, that we want to create an index for even numbers greater than five. We'll merge two conditions to yield this result:
In this query, we used the ampersand (&) to signify intersection - i.e., we're selecting numbers that are both even AND larger than five. Note, that simple and operator won't work here.
Similarly, we can use the pipe operator (|) to signify union - i.e., selecting numbers that are either even OR larger than five:
Awesome, right? This additional filtering layer empowers us to be more specific and intentional about the data we select.
