Master the nuances of slicing and indexing in Pandas by turning a chessboard into a visual data frame. Learn to precisely target rows, columns, and individual cells to efficiently manipulate data for practical applications.
Key Insights
- Demonstrates how to select specific segments of a Pandas DataFrame, such as extracting the lower right 5x5 or the upper right 3x3 of an 8x8 chessboard, using the
iloc
method with positive, negative indexing, and slicing. - Explains the concept of stepping by two to select every other row or column, showcasing how to efficiently subset data frames with syntax like
df.iloc[::2, ::2]
. - Illustrates practical usage of Pandas indexing by setting specific DataFrame sections, such as the middle four rows as empty spaces or assigning chess pieces—like pawns and rooks—to their starting positions on a chessboard represented as 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.
Challenge: get the 4x4 in the upper-left corner of the chessboard df. So that would be 4x4 upper-left equals chessboard_df.iloc. We want the upper-left 4, so we're going to start at 0, go to 4. Start at 0, go to 4 on the columns as well.
Save that as a new df. And there it is—that's the upper 4. Let's get the 5x5 in the lower-right corner of the chessboard. So 5x5, you're going to go into that 64 pack and get the 25 of them in the lower-right corner, which would be here.
There's five—those five all the way down—5x5,25 out of 64, a pretty big slice. So how do we do that? We would say 5x5 lower-right is going to be −5 to the end, −5 to the end, or you could say 3 to the end. Wait, why does it say ddfgh? Did I do d twice or something when I made the chessboard? Yeah, I did.
Okay, no e. All right. All there. Or you could say, let's start at 0, go to—no, don't start at 0—start at 3, go to the end; start at 3, go to the end.
That would also work, ddfgh, 5,4, 3,2, 1. That's lower-right corner, 5x5. Challenge: make a new df called tttu, upper-right df as a tic-tac-toe board using the upper-right 3x3. Okay, turn off and try it.
It's going to be the chessboard_df.iloc, first three rows, last three columns, right? If you're trying to get the upper-right. We don't have to call it ttt; we can call it 3x3 upper-right, like so. So pause the video, try to get the upper-right 3x3 from the 8x8—exactly like we just did here where we grabbed the lower-right 5x5.
All right, pause. Okay, you paused; now let's do the solution. Chessboard_df.iloc—we want the upper-right, so you want the first three rows and the last three columns. I believe that ought to do it.
Yep—upper-right, first three rows, last three columns. Oh yeah, that's right: 8,7, 6—that's the first three rows; f, g, h—that's the last three columns. :3
for the first three rows, , -3:
for the last three columns. How would you grab every other row? Okay, let's try that.
That would be a step. We'll say, every other row: chessboard_df—that's kind of long, but oh well—we're going to say we want every row, every column, and we want to go by a step of two, and that's an error. We really want to say that, okay, ::
. If we run it, there you go—it’s every other row: 8,6, 4,2. If you wanted every other column also—there: aceg. The rows you would want in every other row would be 8,6, 4,2, and the columns would be a, c, e, g, like that, because we're stepping by two.
Start at the beginning, go to the end—that's what the double colon is for. Two is your step—every other. How would you grab every other row and every other column? We just did that.
How would you grab every other row, all columns? You just wouldn't specify a second value: every other row would be ::2
—that's every other row, all columns. Then every other row and every other column—we're going to do the double skip; there you go. Here, you get every other row but all columns; here, you're getting every other row and every other column: ::2, ::2
, because you're repeating that—everything's stepping by two on rows and columns. I know it's confusing; it's difficult; you'll get used to it—you'll have to practice.
loc
and iloc
are tricky. Set the middle four rows, all squares, to empty spaces. To get the middle four rows, we could say chessboard_df.iloc[2:6]
, right? There you go—there are your middle four rows. But we want to set them equal to empty spaces—wait, oh, incompatible data type, right, right, right.
It doesn't want to do that, but it does it anyway, because the data types of the values in those squares were numbers, and I just set them all with strings. So, even though it looks kind of scary, it did work. If we run it again—there, they're empty.
It just didn't like going into numeric data and replacing it with strings, but it did clear them out anyway. That was not an error—it didn't turn red—but it was a warning that you just saw. We're trying to get our chessboard set up the way it is at the beginning of an actual game, where you don't have anything in the middle four rows.
That's kind of the no-man's-land at the start of the game. So, we've got that. We've cleared out the four middle rows of the chessboard.
Now, we want to set the pawns. We're going to target 7 and 2, which are the pawn rows. I don't know if you know how to play chess, but the pawns are the front row of either side.
You have eight pieces, so White and Black each have 16 pieces, with the front row at the start of the game being all pawns. So, rows 7 and 2—or 1 and 6, if you're using integers from 0 to 7—are set equal to 'p'
for pawn, another string. It might not like it, but maybe it's used to the fact that we're using strings now.
Set the pawn rows to 'p'
, like a chessboard at the start of the game. We'll say chessboard_df.iloc[1]
—that would be Black's pawn row—zero, one, and 6
for White's.
So, 1
, the Black pawn row, is set equal to a pawn, and the White pawn row at 6
is set equal to 'p'
as well. If we run that, we've got pawns now—p
for pawn.
Why are we doing this? We're learning how to target in a grid system—how to find rows and columns and cut them, slice them, and extract them, but also target them with changes. So, we've got blanks and pawns. Now, what about setting the four corners to rooks? There's a rook in all four corners to start the game.
Rooks are the castles, and all four corners are castles. We want to replace those with 'r'
s for rook. How would we do that—like a chessboard at the start of the game?
Well, we would say chessboard_df.iloc
. We want to go to row 0
, and then for columns, we're going to say we want column 0
and column 7
; since they're not contiguous, we feed that in as a list.
They don't touch, and row 7
follows the same idea—and it didn't work. Okay, why not? Oh, because we didn't set them equal to anything. There you go.
There are your 'r'
s in the corners for the rooks. Let's break it down. We're targeting integer location.
We're going to row 0
, which is integer-wise—from 0 to 7—not the 8-to-1 backward labels. That top row there is row 0
, and we want to go to item 0
and item 7
. Since they're not connected, we can't say go from 0 to 7.
That would grab every single one. We just want to go to 0
and 7
, which are not contiguous, consecutive, or touching, and for that, we feed the column names in as a list. That allows us to hit non-touching list items.
So, we want to do column 0
and column 7
in row 0
, and we want to go to row 7
, the last row, and also do column 0
and column 7
—the outer two columns—set them equal to 'r'
s, and it works.