Reshaping Workout
- 03:29
Practice manipulating and reshape arrays in Python using NumPy.
Downloads
No associated resources to download.
Glossary
NumPy Numpy arrays Python ReshapingTranscript
In your Jupyter Notebook, you'll find array A already defined for you. Start by importing NumPy into your Jupyter notebook if you haven't done that already. And then execute the cell to define array A. Now complete the following steps. First, create a new array, array B by reshaping array A to six rows and four columns. Then print array B. Finally, using ranges, print New York stock Exchange from array B with a new line for each word. And that means you're going to call the print function four separate times for each word.
Our first step is going to be to execute this code cell that defines array A. Now we're going to reshape array A. So it has six rows and four columns, and I could define this as array A and, and just change the shape. But what I'm gonna do here is create a new array called array B. If you didn't do that, that's totally fine, but I'm gonna define array B as array A reshaped with six rows and four columns.
And to make sure I've done that correctly, I'm going to print array B. It looks like that was reshaped correctly, and now it's going to be a lot easier to select the letters that I need to print out New York Stock Exchange. At this point, this is just an exercise in selecting ranges. We're printing one word on each of these lines on this first line. The zero index tells us that we are looking at the first row here.
We want to start at the beginning, so there's no index. And then we want to exclude index three, which is that Y. Next, I wanna print the word York, which is this Y, O, R and K all in the last column, but across separate rows. So for the rose argument, I'm starting at index 0, so I don't need to put anything there for the Y. And I'm excluding index 4, which is the H right there in the columns argument, I'm telling Python that we only want to look at index three, which is this final column for the word stock. We need two ranges. We need S, T, O, and then we need C, K on the next row. So we're going to row two, which is index 1, starting with the second column that S, and then going all the way to the end, which is why that's blank. And then we are concatenating that with a comma to the C and K. So we're going to index two, which is this third row. We wanna start at the beginning, so there's no index there. And then we want to exclude index two, which is this underscore.
Finally, for the word exchange, we want the entire fifth row concatenated with the entire sixth row. So here we're using index 4, and then we're just putting a colon, there's no index, and that tells Python that we want the entire fifth row. And then we're pointing toward index 5, which is the sixth row, and doing the same thing, just a plain colon, which tells Python we want the entire row.
And when I print that out, you see I have each of the words on a separate line.