Unlock the power of precise data selection with Pandas' iloc method. Learn how to effortlessly extract specific rows, columns, or subsets from your DataFrame using intuitive indexing techniques.
Key Insights
- Understand that Pandas' iloc method uses integer-based indexing, allowing precise extraction of data subsets, such as selecting the middle four rows (indices 2–5) and middle four columns (C–F) in a DataFrame.
- Discover the differences between retrieving data as a Pandas Series versus retrieving it as a DataFrame, noting that Series retain column headers as indices, creating key-value pairs.
- Learn practical applications of iloc through hands-on examples, such as extracting the first row, last row, or specific interior blocks of a DataFrame.
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
This is a lesson preview only. For the full lesson, purchase the course here.
Onward. We output the DF already. Okay, now, how would you go in and get individual bits and pieces and selections and a tic-tac-toe board’s worth of data out of this 8×8? Well, let’s start with extracting an entire row.
Let’s get the first row—we’ll say row 0, right—is going to equal chessboard, or first row, let’s call it. It’s actually row 0 now, right? It has a name. First row is chessboard DF.
We’re going to extract 0, just like so. And if we print that, and we print the type of that, and we print the length of that, we got an error—not supported. We want row 0, we want the first row, row 0, all columns.
There we go. Now, that gave—and we don’t actually want to print that, we just want to run it. Yeah, let’s go like so.
It’s going to look better if you don’t print it. No, that would be type—actually, type. Yep, it’s a DataFrame.
First row. Oh, it’s going from 0 to the end. We don’t want to do that.
We really just want—what are we doing here? Okay, what in the world? All right. All right, we need to use what is called loc and iloc now. Note the name iloc.
So, you don’t just say DataFrame, you say iloc—integer location. There you go.
That’s row 1. Extracted, right? Row 1. And if you print row 1, there you go. Now, it’s the numbers 1 through 8, which indeed is row 1, but it’s printing it kind of differently, and it’s also showing the letters. So, what’s going on? It’s actually preserving the reference to the columns, right? 1 to 8 are just not numbers.
They are the values of these eight columns. So, when you extract a series, it remembers the column headers and brings you those. So, basically, what you’re getting are key-value pairs.
So, we could print the dictionary version of first row by passing it to the dict method, and there you go. First row dictionary. The first row is extracted as a series, and let’s start like this.
Okay. So, we have a series. The data type is Series.
The length is 8, and here we have A through H. Those are our key-value pairs, the values of which are these ints, but we’re going to listify the ints. Can I convert dictionary update to sequence? Okay, fine. Okay.
Iloc 0. Great. So, it is a series. Let’s listify, yeah, row here.
I’m going to take it from the top. We’re going to say row 0 series is going to equal chessboard df dot iloc—for integer location—at 0. We’re going into position 0. So, even though it says 8, it’s really 0. It’s a 7 under the hood, right? So, because it’s a list.
It’s an array. It’s still 0. Then we’re going to print. Let’s print the data type of that.
Let’s see what we got so far. It’s a Series. Okay, great.
And let’s get the listification. Let’s get the length of it. It’s 8. Great.
Let’s listify it 1 through 8, and let’s get the dictionary version of same, and it’s coming out like that. Why does it know to break it down? Why is it calling it? I’m just a little confused here. It’s saying A. The value of A is this NumPy 1, NumPy int64.
It’s a Series. What if we just print the actual thing? That will be the Series, right, like so. I want the dictionary version of same.
I’m just a little confused why it’s saying A and not just a 1 after. Why is it going np int 1? Let’s print the shape, right? The shape is 8 comma nothing burger because it’s a vector. Again, this dictionary of A through H, I get it.
The keys are A through H, but the values are np int64 8 as opposed to just 8. All right, fine and dandy. Let’s keep rolling. All right, we’re going to get the first row as its own DataFrame now.
Getting the first row as a vector with iloc—that’s fine—but what if we want to get the first row as its own DataFrame as a 2D matrix? To do that, you don’t just say 0 here. You would ask for a two-dimensional response because a DataFrame is 2D. We’re going to say row 0 df, as in DataFrame, and that’ll be chessboard df dot iloc for integer location.
We’re going to say we want all rows to row. We’re going from row 0 to 1, and let’s print the shape, the type. The type ought to be DataFrame, and then we’ll just print the thing.
Yep, so it’s a 1×8 DataFrame—one row of eight columns—and remember 8 is the first row, right? It’s up at the top, row 0 basically. Let’s get the last row as its own DataFrame. We’ll say row 7, right, being the last row, or just say last row.
Last row DataFrame is going to be negative 1 to the end, right? We could say negative 1 to the end for rows. That’s the last row. Remember, we’re counting backwards from 8 to 1, so the last row is row 1 labeled 57,58, and so on.
There we go. Remember, negative 1 is the end, so we’re going from negative 1 to the end of rows. Challenge.
Let’s make a new df in the middle. We just want the middle four rows of all columns, so we’re going to start at index 2 and go to index 6, inclusive. So pause, work that out, and come back.
It’s a range from 2 to 6, except now it’s rows of a DataFrame. All right, here we go. Mid four rows df equals, let’s say, chessboard df dot iloc.
You can’t just directly call on this thing with the square brackets. It’s iloc, and we’re going to say 2 colon 6, print the shape of that, and then the thing itself, and it’s 4×8, right? Because we want middle four rows, all columns. Those are the middle four rows, right? Not 8 and 7, not 2 and 1, but we’re going by index, right? We’re not looking them up by these label names we applied.
It’s still 0 to 7 for the row numbers index-wise. Let’s make another challenge. Now let’s do the middle four rows, middle four columns—so do your four rows that you did, but now come in and just grab the middle four columns, those being C through F. It’s the same principle, 2 to 6, but it’s 2 to 6,2 to 6. We’ll say pause, do it, and come back.
OK, here’s the solution. We’re going to say mid four rows, mid four columns—or we’ll say mid 4×4. How’s that sound? The mid 4×4 is going to be: we want rows 2 through 6, but then comma, we want columns 2 through 6, because it’s a two-dimensional selection, and there you go. You’ve got your middle four rows and your middle four columns.
So it’s iloc row range, comma col range. Let’s go look at that. Row index, col index, middle four select—oh no, we don’t want non-contiguous, sorry.
Oops. Oh, we’re fine. OK, so 2 to 6,2 to 6. Let’s see, where’s the little—we do want contiguous, though.
All right. Iloc—well, it’s row—it’s really this: it’s row index, row start index, colon, row end index (exclusive), comma, col start index—a lot to write, but this is how it is—colon, col end index (exclusive).
So what that means is, if you want to select a 4×4 pack out of the middle like we just did, you start at row index 2 and go up to but not including 6, comma, then the col start index and the col end index (exclusive). So basically what we just did with iloc, let’s do another one. Here.