Locked lesson.
About this lesson
Just like iterating through rows, we can also iterate through columns.
Exercise files
Download this lesson’s related exercise files.
21 - Existing Excel - Iterate Thru Columns.docx60.4 KB 21 - Existing Excel - Iterate Thru Columns SOLUTION.docx
57.8 KB
Quick reference
Existing Excel - Iterate Thru Columns
Just like iterating through rows, we can also iterate through columns.
When to use
Use this method when you want to iterate through columns with more control.
Instructions
for col in ws.iter_cols(min_row=1, max_row=4, max_col=1, max_col=2, values_only=True):
print('\n')
for cell in col:
print(cell)
Hints & tips
- for col in ws.iter_cols(min_row=1, max_row=4, max_col=1, max_col=2, values_only=True):
- 00:04 Okay in the last video we looked at another way to iterate through rows.
- 00:07 In this video I want to show you another way to iterate through columns.
- 00:10 And it is almost exactly like the last video, except for
- 00:13 instead of entering rows, we're going to enter columns.
- 00:16 So let's create our for loop let's go for, and
- 00:20 let's call this col short for column in our ws worksheet, dot iter_cols.
- 00:26 Remember the last video was iter_ows and our iter_cols, and
- 00:31 this guy acts almost in the exact same way as in the last video.
- 00:36 We can set our man or row to let's say 1,
- 00:42 we can set our min_col to let's say 1.
- 00:48 Let's after this one go, max_row.
- 00:54 And let's set that equal to 8.
- 00:56 And then here min_col=1 and max_col=2, we only have two columns.
- 01:02 And again, this is a for loop.
- 01:04 So we need our colon there.
- 01:06 And we can also if we want to set that flag that we did in
- 01:10 the last video, we can go values_ only=true.
- 01:15 Makes it a little bit easier.
- 01:16 So let's create a for loop.
- 01:18 So let's go for cell and col.
- 01:23 And let's print our cell.
- 01:27 So if we save this and run it, you can probably guess we're
- 01:32 going to get our names and then our favorite pizzas.
- 01:36 So this is a little bit confusing, so like before,
- 01:41 we could set a print statement right here with \n,
- 01:46 stands for newline, right?
- 01:49 Line break and let's clear the screen and run this again.
- 01:53 And we see our columns.
- 01:55 Column one and column two or col A and col B, I should say, and
- 01:59 all of the values inside of here.
- 02:00 And just like in the last video,
- 02:02 we can play around with all of these different numbers if we only want,
- 02:05 let's say, if we don't want the headers, we can set the min_row to 2.
- 02:09 So if we save this, come back here, clear the screen and run this guy.
- 02:16 And we just get the columns themselves without the headers.
- 02:20 If we only want the names for instance,
- 02:23 we could set the min column to 1 and the max column to 1.
- 02:27 And we only want one column, right?
- 02:29 Which is the first column.
- 02:30 So let's clear the screen and this should return just the names,
- 02:34 sure enough just the names.
- 02:36 If we want, the other column we would set that to 2 and 2.
- 02:43 And there we go.
- 02:44 So just another way to iterate through rows and columns your basic for
- 02:48 loop as we did as we did several videos ago works completely fine.
- 02:53 This just gives you a little bit more control over being very specific and
- 02:57 exactly what things you want to do sort of explicitly spelling them out right here.
- 03:02 And sometimes it's just good to do that.
- 03:04 Sometimes you don't need to do that.
- 03:05 So now you have different options to choose from when grabbing information.
- 03:10 But more importantly, now we have several ways to grab
- 03:13 any type of information you want from a spreadsheet.
- 03:15 Be it just a single cell, a range of cells, a single row,
- 03:19 a range of rows, a single column, a range of columns.
- 03:23 And in several different ways to iterate through all of those things.
- 03:27 So, hopefully, this will give you the tools for
- 03:29 any situation you find yourself in.
- 03:31 Being able to grab any sort of snippet of data from a spreadsheet that you need.
- 03:36 So that's pretty much all for grabbing information,
- 03:39 grabbing data out of a spreadsheet.
- 03:42 Now we're going to start to focus on formatting and styling our spreadsheets.
- 03:46 In the next video, we're going to take a little break and
- 03:49 do a quick exercise to put some of these things that we've learned to use.
Lesson notes are only available for subscribers.