Chessboard Setup and Data Frame Creation in Python

Create and populate data frames with chessboard positions and food-related data.

Discover how to dynamically build game elements into a chessboard using Python, then transition seamlessly into creating structured data with pandas DataFrames. Explore practical examples that bridge gaming logic with real-world data handling.

Key Insights

  • Demonstrates how to position chess pieces programmatically on a Python-generated chessboard by accurately assigning each piece (rook, knight, bishop, queen, king, pawn) to specific rows and columns.
  • Transitions from chessboard logic to practical data manipulation by creating a pandas DataFrame with columns for food items, prices, calories, and vegan status, clearly illustrating how lists map to columns.
  • Highlights the similarity between assigning dictionary keys and DataFrame columns, showing how column names in pandas can differ from original list identifiers, emphasizing flexibility and clarity in data representation.

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: On your own, set the knights as "KN" next to each rook, like on a chessboard at the start of a game. You can start with that. Pause, come back when you're done trying.

All right, we want "KN" for knights, targeting the same two rows—the first and last—but we're not targeting columns zero and seven, which are the outer columns. We're coming in a level to one and six because the knights are right next to the rooks. If you look, rooks are at columns zero and seven; knights are at columns one and six. The row doesn't change—just the columns shift from zero and seven to one and six, and you do it in both the first and last rows.

You run that, and now you have knights at positions one and six. Challenge: Set the bishops as "B." They’re next to each knight—same rows, right? We want "B" for bishop, and we're coming in closer, nudging inward to columns two and five.

There's your bishops: rook, knight, bishop. Bonus challenge: Set the kings and queens.

Now you've only got one king and one queen, so the rows stay the same, and the columns are just the ones for the king and queen. Let's go look. That's the king in the e-file.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

Zero, one, two, three, four. The king is in column four, and the queen is in column three. So king is in column four.

Queen is in column three. There's your king. There's your queen.

Oh, wait a second. There are two kings, right? We want to actually target row zero and row seven for column four. Likewise for the queen, we want to target row zero and row seven for column three.

There we go: queen, king, queen, king—and that completes our chessboard. So rook, knight, bishop, queen, king, bishop, knight, rook, pawns, empty spaces, and repeat.

Not so easy. I get it. But we need to learn this stuff. All right, we're going to start with a new empty DataFrame now.

Let's do something more data-ish. That was more like calisthenics—stretching, warm-ups, learning the basics. Now it’s more like game time.

We have some data here we’re going to declare. We're going to turn it into a DataFrame of food. So come on down here.

We're going to say `food_items =` these four items, and we're going to say `prices =` these four items. These are the corresponding prices to the food items. We'll say `calories =` these values.

These are the calories, and "is_vegan" would be a boolean. What we're going to do is take these four rows of four columns. So basically, each list represents a column, and the number of items in each list—four—represents the number of rows.

We'll get a 4×4 DataFrame. We're going to say `food_df = pd. DataFrame()`. We’re making a new DataFrame now—like the chessboard, except it’s food.

We're going to pass it—actually, we're just going to make a new empty DataFrame, which you can do. Then we're going to take the new DataFrame and assign it columns. You declare the columns the way you would assign keys to a dictionary. The name of the DataFrame, square bracket, the name of the column in quotes—just like a dictionary key.

And the first column is going to be the food item values, and the second column will be the price values. We're just taking these lists and assigning them. We're declaring the names of the columns.

Notice we're capitalizing and adding an "s". We're setting the values to be these lists, and the number of items in the list will determine food items. We need prices, calories, vegan—run, run, and run—and we want to print `food_df.shape`. It should be 4 × 4 because we have four rows, four columns. And `food_df`—boom, there’s your DataFrame.

We started out by making four lists of four items each. We made a new empty DataFrame, and just like a dictionary having key–value pairs assigned to it, we declared columns the way you declare dictionary keys and set the values equal to lists. The number of items in a list equals the number of rows in the resulting DataFrame. One list per column, with the names of the columns just declared here—basically like dictionary keys. We're capitalizing, we’re modifying them a bit—they don’t have to match the list names being assigned.

All right, I think that’s enough for now. That’s a heck of a lot. Let me take a little break right now and come back. This is a gargantuan lesson.

I mean, we might be halfway done—it’s pretty long. It goes and goes and goes. So I'm going to pack it in for now. Come back when you are ready. I'll be ready when you're back.

Brian McClain

Brian is an experienced instructor, curriculum developer, and professional web developer, who in recent years has served as Director for a coding bootcamp in New York. Brian joined Noble Desktop in 2022 and is a lead instructor for HTML & CSS, JavaScript, and Python for Data Science. He also developed Noble's cutting-edge Python for AI course. Prior to that, he taught Python Data Science and Machine Learning as an Adjunct Professor of Computer Science at Westchester County College.

More articles by Brian McClain

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram