Loading and Displaying Web Images in Python Notebooks

Demonstrate creating and labeling rows and columns in a pandas DataFrame to match a chessboard's standard notation.

Discover how to seamlessly incorporate and display web images within your Python data frame projects. Learn practical tips for naming and labeling your data frames clearly using standard chess notation.

Key Insights

  • Demonstrates how to import and display images from the web within an IPython notebook using the ipython.display library's Image method, making it easier to visually represent data or concepts.
  • Provides step-by-step guidance on labeling rows and columns in pandas DataFrames by clearly illustrating with standard chessboard notation (columns labeled A-H and rows numbered 8-1).
  • Explains practical Python techniques for reversing lists, such as the slicing method [::-1], to efficiently reorder data without using built-in reverse functions.

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.

All right, we're going to now load up from the web an image. We're going to come up here. We forgot we need more libraries.

We want our pprint. That's kind of standard at this point. Pretty print as pp.

But now we also want something we've never gotten before. From ipython.display, this big library, we want to import image. This will allow us to display images in our notebook.

And we'll import random, which we may end up needing as well. So bake that cake and come on back down. So this URL, let's give it a name.

We'll call it URL. That is the absolute URL on the web of a graphic, a chessboard graphic that we want to load up and display. We're going to pass that to the image method and see if we get our chessboard image.

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.

Yeah, we did. Maybe a little bigger than we need it so we could shrink it a smidge. We'll pass in a width of, let's call it 350.

There you go. So that's a chessboard. Standard notation, A to H for the rows, 8 to 1 or the columns or whatever, the sideways, the ranks, the files, they're called, I think.

So A, B, C, D, that's how they use the notation to move the pieces around. So Queen's Bishop to Knight 3, you'd say 1F to 3G, you're going to move the bishop. So this grid is how you, it's just standard notation in the chess world on how to identify the squares, the 64 squares to provide the notation for moving pieces around.

A through H, 8 to 1, top to bottom, going backwards. So our chessboard doesn't have A to H, 8 to 1 backwards. It's got 0 to 7 in both cases.

So what we want to do is we're going to assign, we're going to build upon our chessboard. Granted, it's got these numbers, doesn't really look like a chessboard. But we'd like to emulate the chessboard's rows and columns.

So that A through H, 8 to 1, we want to come into the 0 to 7 row and column and override it and set it to be more like the real chessboard, A to H, 8 to 1. So as a little challenge, pause, and I'd like you to make a list of code. Can you make a list of the numbers from 8 to 1 backwards? And if we can do that, we can make those be the row names of our data frame. But that would be a challenge for you.

Can you generate eight numbers in a row and can you run them backwards? Pause, come back for the solution. OK, we're back for the solution. So what we would say is, what do we want to call that? We'll say nums, let's say nums 8 to 1. That's going to equal range.

We're going to have to unpack it. List range 1 to 9, because it's exclusive. And then I think we could reverse that, maybe, because it's a list.

We have that reverse method. We got nothing because reverse doesn't return a value. We could say, all right, let's run her backwards.

I don't know if you recall, but we've talked about this exactly once. If you'd like to iterate through, go through a list, start at the beginning and go to the end, but do it backwards with negative 1, that should work. Look at that.

That's how you go backwards. If you had, let's say you had fruits equals apple, banana, cherry, grape, you could run them backwards, right? You could totally run these backwards by saying fruits.reverse, and then you could print the fruits. Let's go ahead and do that.

I need you to do this with me, everybody. Doesn't like something. What doesn't it like? Fruits, mango, I have commas everywhere.

What doesn't it like? Oh, forgot it. OK, fine. There you go.

So there's the number. There they are backwards. Now, you could also do this.

You could just say fruits. Let's turn off the reverse and run it. Now they're in the right order, but you could say colon, colon, negative 1. Colon, colon, negative 1 is for reversing the order.

Reverses order of list. So the two columns means start at beginning. Next column, go to end.

So colon, colon, negative 1 explained. Colon, the first colon means start at beginning. Second colon means go to end.

Negative 1 means go backwards. We're going actually from the beginning to the end. So colon, colon, negative 1 after your list from 1 to 8 runs it backwards without you using the reverse method.

And why do we care? Because we can use that to feed in what are called index names. We can name our rows. We can also want to name our columns.

We're going to make the chessboard DF again, but this time, we're going to supply column headers as letters from A to H. We want A to H as our column. When we made our data frame, we did not tell it to have any columns or rows, right? We just fed in this array of 8 × 8, and it gave us this raw data frame with 0 to 7 as the rows and column names. Because we didn't tell it to give us rows and column names.

Now we're going to redo the data frame, except this time, we're going to provide columns. So what am I doing here? We're going to say chessboard DF equals pd.dataframe, and it passes in the chessboard array again. And this time, we're going to pass in more data, columns.

What do we want for columns? We want, where'd they go, those letters A to H? Should I erase them? Yikes, OK. Oh, no. I thought it, OK, fine.

I had the letters A to H, and they're gone. A, B, C, D, E, F, G, H. These are the eight column headers for the eight column array turned into data frame, and they're numbered 1 through 8. Well, they're named A through H, but there are eight of them. You have eight rows, so you need to feed in eight columns if you'd like to name the columns.

And then the row names are not called row names. They're simply called index. And that's going to be our nums from A to 1. We'll now run that, and there you go.

A through H, 8 to 1, exactly like the chessboard, right? A through H, 8 to 1. So columns and index fed into the data frame method as optional arguments when you make a data frame from an array will give you columns, give you row names. They don't call it row names. They call it index.

And the number of items in the columns array or list and the index list have to match the number of rows and columns in the data frame itself, right? So it's 8. If you try to do it with 7 or 9, right, like that, it will break. Err, shade past is values 8 to 8. Indices apply 8 to 9, right? It's implying that we have nine columns, so make sure they match, right?

When you're labeling your rows and columns when you make a data frame, the number of items in the labels has to match the reality of how many columns and rows you actually do have.

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