Selecting Objects
- 04:37
Learn how to select objects and ranges in NumPy arrays and lists, and the importance of understanding indexing and dimensions for effective data manipulation.
Downloads
No associated resources to download.
Glossary
NumPy Numpy arrays Python SelectingTranscript
Selecting objects in NumPy arrays is similar to selecting objects in lists. So if you remember back to our lesson on lists, lists are 0 indexed, meaning that the first object correlates to 0, the second object correlates to 1, and so on. So if I want the last object, then I can refer to list 1. And then in square brackets that's the fifth position. So index 4, and if I wanted to print that 3, it's in the third position. So list one in square brackets index 2.
As you learn previously, you can select a range of objects inside a list you using a colon. So if I wanna print the first three objects in list 1, first I write the name of the list, and then the first index, and then the last index that I want to exclude. And in this case, since I'm starting at the beginning of the list, you'll remember I could actually just delete that 0 and Python will automatically know that's a range beginning with the first object in the list so that 1, and then excluding index 3. So that'll give me the first three objects in the list.
Selecting objects in a NumPy array is the same except for you have to pass an index for each dimension of the NumPy array. For example, in this table, the number 6 is located in the second row, which is index 1, and the third column, which is index 2.
Here, I've recreated that array in Python. Now don't freak out about the spacing here. I've only done this just for formatting because it's a little bit easier to understand. If I wanted to, I could collapse all of this onto one line and it would work exactly the same. Python doesn't interpret these spaces any differently because they're all indented, and so Python understands that these all go in between these square brackets and parentheses so that we're passing this to our array. But since we have multiple lists, this lets us visualize building that array a little bit easier. So let's say that we wanted to select this 6 in the second row and third column, just like selecting an object in a list. First I need the name of the array here, it's array X, and then I put my indices in square brackets. I want the second row, which is index 1, and then a comma. And I want the third column, which is index 2, and that's gonna give me the number 6.
Selecting a range in an array is similar to a range in a list, except you have to pass two ranges to Python for each of the two dimensions in our array, in this example, we wanna select 1, 2, 4, and 5, and the indices for those are 0 and 1 in the rows and 0 and 1 in the columns. In Python this is a very quick and easy line of code. We point to array X, the array where we wanna select objects from. We start with the starting index 0, which is our first row, and then a colon to indicate a range. Index 2 is the first item that we want to exclude. So in this case, that's the row 7, 8, 9, that's index 2. And remember the way that ranges work in Python, that's the item that we want to exclude. Second, we tell Python the range of columns that we wanna select. Starting with index 0, the first column, and excluding index 2. The third column, when I print that out, it's gonna give me 1, 2, 4, and 5.
Finally, you can use a single index in 1 dimension with a range in the other dimension to get a single row or column. Here we have three examples where we're printing the first row and a range of columns, the third row, and all of the columns and all rows with column 2 or index 1. Now that I've executed this cell, look at each of these lines of code selecting objects in this array and compare it to the array that we've created up here to make sure that you understand how this is working. In just a second, you're going to try and exercise where you're gonna put this into practice on your own. If you need to pause the video now, just to review and make sure that you understand how this works. And if you're ready, let's go ahead and move on to the next exercise.