Python For & While Loops for Data Science / Data Analysis - P.3
Key Takeaways
This video covers the basics of Python for and while loops for data science and data analysis, including iterating over sequences, using the range function, and controlling loop execution with break and continue statements. It also demonstrates how to read and analyze CSV files using the csv module and pandas library.
Full Transcript
what up data nerds i'm luke and welcome to my channel where i make data visualization easy in this video today we're going to be going over loops specifically the two types of loops for and while loops we'll be spending the majority of our discussion on for loops because i feel these are more dynamic and used more often from there we're going to get into flow control statements so certain statements we can use within loops to control loops and then finally we're going to wrap it up all together and do a practice problem using the office script data set and we're going to be analyzing to see if we can find certain statements within the script itself and see how often they appear so with that let's jump right in so here we are in my mac on vs code with a jupyter notebook launched and the first loop we're going to be discussing is for loops with the for loops we have this basic structure and it's going to be where we have four the itter and that's just the variable we're going to use to iterate through some sort of iterable followed by a colon and the next line needs to be indented for it to carry out whatever the force statement is and as long as we have something to iterate over the code underneath it will execute so here is a simple for statement and it's designed and set up just like we previously discussed you have the variable that we're going to be using to iterate through some interval in this case the interval that we're going to be using is a list of numbers so 0 1 2 3. and as it goes through each one of those numbers those numbers will print to the screen so if i press shift enter i can see hey zero one two and three one thing that confused me was this uh this this variable name right here so this can be anything right i can change this to uh if i wanted to i can change this to x and what's gonna happen is as long as i'm using it still within the statement right here it's still going to execute the same code additionally we don't necessarily have to put the full iterable inside of here you can assign it to a variable and we can make a simple looking statement like this and we're still going to get the same results for loops it's important that you understand the built-in python function range as you'll see this whenever you're reading other people's code and you'll even use it in your own code but if i go ahead and insert range four and do shift enter okay we'll output hey it's this range zero through four and this is actually an iterable so if i change this into a list press shift enter i will see hey it's 0 1 2 3. so actually applying this inside of a for statement i'm going to go ahead and paste this right here and i've made this a little bit more dynamic in the fact that hey we have this four number in range and you can actually have up to three different parameters specified to range so the start number the stop number and then the um the space that you want to do in between those numbers so two in this case so if i do shift enter for this i would expect yeah four six and eight when using loops it's important understand that python can iterate over any items of any sequence so what that means is it could be like a string it could be a list a tuple in this case we're going to do a dictionary and so say we have this ages cast press shift enter so it's evaluated in there and then if i wanted to loop over this aegis cast i could just make a statement where we're going to cycle over the keys inside to aegis cast and for this we're going to print the keys so yeah so as this goes through it will just cycle through and and do the names itself which are the keys but say we want to get the which we're going to take this a step further now say we wanted to get these uh the values for this how will we do that so to get it we have two main approaches that we can take we can either call into the key of the dictionary itself or iterate over the dictionary items and we're going to explain this further so let's do the first case first of using the actual keys itself so if i had the ages of the cast and i wanted to look at say dwight's age i could put dwight within the brackets here and it's going to call out what is the value for this key and we look up at the this right dwight is 48 so the key dwight the value 48. so we could do a similar approach whenever doing the for statement we could do for key in ages cast and then from there i want to print yep the key and then also the ages cast i'm going to put the variable inside of the aegis cast to actually put the different names as we cycle through this loop so if i do shift enter we can see that it actually goes through and not only prints the keys but also prints the value for it so the other approach using the dictionary.items is what you'll probably see more frequently and just to understand what the dot items is so let's do the ages cast dot items and see what comes up press shift enter and what it is is it's a dictionary items it's a list of tuples so you have your dwight and 48 gym and 34 and it has the key values within each individual tuples now this is great because now i can do a for statement and i'll do this right i have two items within this list right so i put these two variables right here and it's going to pull them out of this ages cast items itself and then from there we can print the key and the value and i go ahead and press shift enter and yeah so then it prints the the key value pairs from that dictionary and so just don't get confused you can put you could put multiple variables especially if it's something like a a tuple that has multiple values within a list say the last built-in function that we're going to be covering is enumerate that and you'll see this come up from time to time but let's say we had it could be a tuple a list or dictionary but in this case we're using a tuple let's say we had a list of certain items and they appeared in a certain order and we wanted that index to appear next to it so in this case hey these characters are ordered by the number of episodes they appeared in with pam bean and the most episodes of meredith being in the least so when i print this i want it to print basically one for pam and then four from meredith and using the current approach it's not printing this so let's use enumerate to get this so if i type in enumerate and do female cast and shift enter we can see it's a enumerate object and investigate it further i can do i can convert this into a list to see what's going on here and we can see oh okay hey zero pam uh one angela two kelly and three meredith nice okay so let's go ahead and actually insert this into a for statement of how we want to it to appear okay so here we are with the for statement and what we do is we want the to have the title of the highest number of the office appearances then from there cycle through the index and the value in this enumerate remember previously we did the dictionary where it did the key and the value in this case we're gonna have the index and the value come out of this using the enumerate built-in function and then from this we're going to want to print this i did a index plus one because i don't want it to say zero pam i want to say one period pam and so on and so i do shift enter and yeah we can see hey highest number of office appearances one pam two angela and so on so moving on to the second type of loop a while loop here is the basic structure and it's gonna be wild and then you're gonna have a condition and this condition needs to evaluate similar to an if statement needs to evaluate to either true or false and so while the condition is true the condition below it or the code below it that is indented will continue to execute so let's see an example of this so here's a basic example so while true print dwight was here and what's going to happen is is unfortunately we're going to go into a never-ending loop um because it's going to continue to be while true and continue to print this and so if you ever find yourself in this especially you can see right here with this star right here just go ahead and stop the notebook itself and once it's stopped it will you won't be in this infinite loop anymore so let's just modify this code further to fix that so we've modified this code to take that permanent true condition out and set it to a variable and from there we expect that this loop to print dwight was here once and then from there set the condition to false so we'll exit the loop after this so pressing shift enter yep it just printed once moving into a more complex condition if you will if you're coming from previous programming languages such as pyth vba for microsoft you may see this as very familiar and we use while loops to uh cycle through to a certain number so in this case we set x equal to zero and while x is less than three we want to print the certain variable and every single time we go through this while loop we're going to increment it by one so if we do shift enter yeah we expect it to iterate through each time until we get to three and then it exits the while loop i would encourage you though if you're ever writing code similar to this uh this this statement uh right here if you're writing code similar to this i would encourage you to use for loops instead because we can get the same job done pressing shift enter with less code and with less likely to have mistakes within it now let's move on to loop control statements and as i was talking about the intro these are statements and keywords that you can use within your code to control the flow of your loops so let's look at the first example the first example is going to be using break let's say we have this tuple of female cast members and we're going to we're going to iterate through it so four females in female caste however if we get to kelly within this tuple we do not want to continue on with listing or carrying out the code so in that case we would use a break statement and whenever i press shift enter we can see that it good it does exactly that so it goes through pam it goes through angela as soon as the kelly condition is met it breaks from the for loop and exits and therefore does not execute this print female for kelly or for meredith thinking about our previous example where we got stuck in that infinite loop breaks or statements are also good at making sure that you don't get within this infinite loop so if you wanted to when you're writing your code initially and testing it out you may want to insert break statements just in case to make sure that you don't get into an endless loop in this case it demonstrates the hey it does it prints the dwight was here once and then it breaks and exits the loop so now moving on to continue statements so break was used to exit the loop continue is just used to skip over a certain certain lines of codes that follow so a very similar example we're still iterating through this tuple list of female characters when we get to this kelly name we just want to skip kelly we don't want to print it to the screen and we want to basically skip the rest of the statements within this for loop and start with the next item so meredith so whenever i go to execute this shift enter i would expect to see yeah pam angela and then it gets to kelly does the continue and it will go back into the top of the for loop skipping the print female for kelly and then executing meridith and then the final flow control statement to talk about is pass not use this frequently but say that you are you know you want to have this while statement and you know you want to have a while statement for a certain condition but you don't really know what you're going to be using within the code within it if i i have to put something underneath it in order for it to not throw this error so pass is a great way to do that and what it's going to do is just pass but in this case once again we're in this infinite loop so we need to get out of this so let's move on to the application of loops within a data analytical problem so we have a data set for scripts for the office and it's located right here within our repository itself and what we want to analyze is we want to see how many times in the script they have the saying that's what she said additionally we want to not only find when they say that but we want to return the previous and the current lines to see what's so funny about it and just one little note on this you can uh use external libraries such as pandas but this series is focused on just using the python standard library so that's where we're going to focus on this but feel free after this to use uh to use pandas and if you don't know what i'm talking about don't worry too much about that for the data set itself just looking at the csv imported into excel it has basically for all the different seasons and episodes it has every single line and it has the speaker of who it came from so we're going to be searching through this column e if you will to find uh that condition so as always let's uh let's go over what the approach is going to be so the first approach for the first portion we need to basically ingest all the data from the csv once we have that data inside of the file we need to then go and extract only the script lines and what we want to do is just put this into a list now that we have this list we want to go through the list and check for the statement of that's what she said and then finally once we check for that check for that match statement uh that's what she said we want to return the previous line and also that line itself okay so let's jump right in so for the first portion you're probably wondering hey how do i import a csv and i just want to go through the basic approach that i would take any time i'm encountering this type of problem and that's just google it and typically whenever you google anything i would try to go to the docs.python.org and they also have other good references as well but i like using docs.python.org then from here we can see the the module itself we can use within python to import a csv and we'll be using that so for this we're going to first need to import the module csv this is part of python standard library so we don't need to do any pip install from there we want to open the csv so i'm just going to put it paste this right here but we're going to use the python built-in function open and we specify we're going to specify it's within the directory so it's within this the office data so i have the office data backslash scripts the office dot csv and then one thing to note right this is a string so you need to encompass it within single or double quotes and then have open any time you open a file you should also for a good practice close the file so you would use the method close and then if i want to take it further and print csv file to see what's going on here i can do shift enter and so it's an input output wrapper we can't really do anything with this so we need to take make it into basically a uh if you will a reader uh type file so we'll do csv reader and this is where we're going to use the csv module so we'll do csv dot reader and then from there we'll use csv uh file itself and then we can also do similar we can print csv reader okay so now we have a reader object and this is a reader object is just for your information this is now something we can iterate through and that now we're going to use in a loop to actually iterate through the contents of this iterable so i'm going to modify this print statement further i'm going to do a for loop and so i've added this code so we're basically going to go through each row in csv reader we're going to print that row and then we could print all of the different rows but remember we used break before i just want to see the first row and then break from this loop and see what's printed out so we do a shift enter and see we see hey okay this is the the first line of the excel file and so or the csv file so we're on the right track so remember our approach to this we want to read the data from the csv which we have now we want to extract uh we want to extract script data into a list and so that's the next thing so we want the script data remember that the the script data is in this uh this line text and if we count over we can see that it is the so one two three four five items over and remember python uses zero as an index as we saw previously so i want to print the uh number four and let's see what happens whenever we do this we do shift enter i would expect to get yeah line text okay so now we know we're gonna be getting the the fourth item uh of every single row as we go through this now what we can do is we can have this added to a list so what we'll do is we'll add in a list and we'll just set this equal to a blank list so there's there's nothing inside of this list it's just blank and then from here we can say hey we will do list dot remember we can you do append to append something to a list and we'll do that row and then that fourth item and then i can get rid of these items the that are attached so we're just going through and basically it's going to it's going to get all of the different scripts into one single list and we do shift enter okay we can't really see anything the list is going to be pretty big but we'll just go ahead and print it so list and yeah so we can see it's just a giant list of all the different scripts here so now we have these this list like i said of the the scripts itself now we want to iterate through it to perform the next step we want to check for the match statement which is that's what she said so first let's define the condition so the condition we want to meet is a variable and we'll just sign it okay so we've assigned that to a variable and what we want to do is we want to loop through this list and check for each item in that list to see if it has that this this statement right here this condition so i'll do a for loop first for each item i want to see if that it contains or it has uh this certain uh phrase within it and remember we can we can also use the that in operator to check for that so if condition is in item let's do print the item so yeah this is uh just a recap right this is going through each item in the list and then from there it's doing a conditional statement that says hey if that's what she said is inside of that portion of the list wants you to print it so let's uh look at that all right and so now we're getting a good bit of results and okay we can see all the different ones and it looks like everything's matching on that's what she said but then going back up here remember we want to check for the match statement which we've done now we wanted to return the previous line and the current line so right now we're returning the current line but we want to return the previous line so what i will do is we will assign something so we'll do this and we'll assign a variable outside of this if and we'll have previous item equal to item and then from here we'll be able to as this loops through this will have previous item for the previous one if you will so i can do a previous item okay so i've updated this further and just as a quick recap we're going to be printing the previous item so what was the item before the item and then form a new line that's what this is right here just signifying a new line then the item itself and then two new new lines so that way we have space in between all of this and and it's more in a readable manner so if i do shift enter okay we can actually see um and go through uh yeah so we have some i'm not gonna say this out loud because it's a little bit inappropriate but we have all of the different uh that's what she said statements and have the previous line on on what triggered that that's what she said statement bam so there you have it uh we covered loops a day focusing on four and while loops and then also looking at control flow statements just to reiterate i feel the flow of for loops are more important when they come to data analysis and i use them more often than while loops in the next video we're going to be talking about list comprehension which can also be used in dictionary and tuple comprehension but anyway we're going to talk about this python type of way to make lists in the easiest manner possible that builds on the for loops that we learned from this video so as always if you found this video useful smash that like button comment down below on if you have any questions relating to loops and if you're finding this tutorial series interesting please consider subscribing and with that we'll see in the next episode
Original Description
Python Fundamentals Course (DataCamp) 👉🏼 https://lukeb.co/PythonBasicsDataCamp
Data Analyst Track w/ Python (DataCamp) 👉🏼 https://lukeb.co/PythonAnalystDataCamp
(My recommended courses that I took to learn Python!)
This video provides and overview of how loops (for and while) are used by data scientist and data analysts. In this tutorial we take time to cover for and while loops and how they are used with different built in functions such as: range(), enumerate(), & .items(). Additionally, we cover loop control statements: break, continue, pass. Finally, we combine what we learned to analyze the script from the popular TV show "The Office" to find lines that contain: That's what she said.
Playlist (Course) Material 👉🏼 https://github.com/lukebarousse/Python_Intro
00:00 Intro
00:41 For Loops - The basics
02:08 For Loops - range() function
03:07 For Loops - Iterating over objects
03:53 For Loops - Iterating over a dictionary
06:10 For Loops - enumerate() function
08:01 While Loops - The basics
09:20 While Loops - More complex
10:17 Loop Control Statements - break
11:43 Loop Control Statements - continue
12:29 Loop Control Statements - pass
13:02 Application: Analyzing "The Office" script
14:54 Application: Read the CSV
17:04 Application: Insert Column from CSV in list
19:42 Application: Find "That's what she said"
22:50 Conclusion
Basics of Python playlist 👉🏼 https://www.youtube.com/playlist?list=PL_CkpxkuPiT8zTx9XO9jFf4PQ0iwBWfXB
Setting up Python on your computer playlist 👉🏼 https://www.youtube.com/playlist?list=PL_CkpxkuPiT9udgCeqZpS4HKF6uIzra3r
Recommended Certificates/Courses (Affiliate Links)
==================================
DataCamp:
Python 👉🏼 https://lukeb.co/PythonBasicsDataCamp
Power BI 👉🏼 https://lukeb.co/PowerBIDataCamp
Tableau 👉🏼 https://lukeb.co/TableauDataCamp
R 👉🏼 https://lukeb.co/RDataCamp
Data Analyst w/ Python 👉🏼 https://lukeb.co/PythonAnalystDataCamp
DataCamp Subscription (Monthly $25USD) 👉🏼 https://lukeb.co/DataCam
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Luke Barousse · Luke Barousse · 19 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
▶
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Connect Google Sheets to Tableau & Joining Data - Tableau Tutorial P.1
Luke Barousse
How To Use Tableau Desktop Controls - Tableau Tutorial P.2
Luke Barousse
Dimensions Vs Measures (Blue Vs Green Data) - Tableau Tutorial P.3
Luke Barousse
Create Stacked Bar Chart (and any other visuals EASILY!) w/ Show Me! - Tableau Tutorial P.4
Luke Barousse
Conditional Format Tables in Tableau (Like Excel!) - Tableau Tutorial P.5
Luke Barousse
Calculated Fields in Tableau (Formulas & IF Statements) - Tableau Tutorial P.6
Luke Barousse
Parameters (Create & Use in Calculated Fields and/or Visuals) - Tableau Tutorial P.7
Luke Barousse
Totals, Average Lines, & Trend Lines (Analytics Pane) - Tableau Tutorial P.8
Luke Barousse
How To Create a Dashboard - Tableau Tutorial P.9
Luke Barousse
Upload your dashboard to Tableau Public - Tableau Tutorial P.10
Luke Barousse
Install Python for Data Science on Mac & Windows (PC) with Anaconda - P.1
Luke Barousse
How to run Python for Data Science - Editors vs IDEs - P.2
Luke Barousse
Install VS Code with Python for Data Science / Data Analysis - P.3
Luke Barousse
Understanding Virtual Environments for Data Science / Data Analysis - P.4
Luke Barousse
Using VS Code with Python for Data Science / Data Analysis - P.5
Luke Barousse
Python for Data Science / Analysis ft. 'The Office' Dataset - P.0
Luke Barousse
Python Objects frequently used in Data Science / Data Analysis - P.1
Luke Barousse
Python If Statements for Data Science / Data Analysis - P.2
Luke Barousse
Python For & While Loops for Data Science / Data Analysis - P.3
Luke Barousse
Python List Comprehension for Data Science / Data Analysis - P.4
Luke Barousse
Python Functions for Data Science / Data Analysis - P.5
Luke Barousse
Lambda Functions for Data Science / Data Analysis - Python P.6
Luke Barousse
How NOT to learn Python for Data Science
Luke Barousse
What is Business Intelligence (BI)? 📊😅
Luke Barousse
Top 3️⃣ Technical Skills for Business Intelligence 📚📊
Luke Barousse
Top Non-technical Skills for Business Intelligence 📊👨🏼💻
Luke Barousse
M1 vs Intel Mac for Data Science
Luke Barousse
M1 vs Intel Mac for Excel 📈👨🏼💻
Luke Barousse
M1 vs Intel Mac for Python 🐍👨🏼💻
Luke Barousse
M1 vs Intel Mac for Business Intelligence Tools 💻📊
Luke Barousse
M1 Macbook Air vs Pro (8 vs 16 GB) for Data Science
Luke Barousse
Python for M1 Mac vs Intel (SPOILER: M1 is 2x faster)
Luke Barousse
Data Analyst's WFH Setup & Upgrades
Luke Barousse
Windows on the M1 Mac - What are your options?
Luke Barousse
Install your favorite Windows app on M1 Mac - ft. Parallels
Luke Barousse
Data Science shortcuts for Mac
Luke Barousse
Day in the life of a data analyst
Luke Barousse
Power BI vs Tableau - Best BI Tool
Luke Barousse
Mac Vs PC - BEST for Data Science
Luke Barousse
Data Scientist vs Data Analyst (funny!)
Luke Barousse
Become a DATA ANALYST with NO degree?!? The Google Data Analytics Professional Certificate
Luke Barousse
Certificates vs Degree for Data Analysts (ft. Google Data Analytics Professional Certificate)
Luke Barousse
Google vs IBM Data Analyst Certificate - BEST Certificate for Data Analysts
Luke Barousse
Python Vs R (funny!)
Luke Barousse
THIS got me my job as a Data Analyst - My portfolio tip
Luke Barousse
I used Python to Count my Bike Jumps!
Luke Barousse
Standout as a Data Analyst with THIS TOOL
Luke Barousse
STOP using Spreadsheets for Everything!
Luke Barousse
Transition into Data Science - My Tips & Story
Luke Barousse
Get a JOB w/ Google Data Analytics Certificate?!? (ft. Certificate Holders)
Luke Barousse
Staying Motivated in Data Science
Luke Barousse
Data Science - Expectation vs Reality (funny!) - ft. @KenJee_ds
Luke Barousse
Get NOTICED in Data Science!!! (3 types of GREAT projects)
Luke Barousse
Use THIS to showcase EXPERIENCE in Data Science
Luke Barousse
How to show EXPERIENCE... when you have NONE?!?
Luke Barousse
Learn PYTHON to be a DATA ANALYST?!? (or is R enough...)
Luke Barousse
The BIGGEST MISTAKE when starting a data project!
Luke Barousse
Top Jobs in Data Science
Luke Barousse
How to get Data Analytics side jobs - NEW LinkedIn Feature
Luke Barousse
Building a bot to scrape job data… How NOT to collect data
Luke Barousse
More on: ML Pipelines
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
The Attention Economy: Your Attention Is Worth More Than Gold
Medium · Data Science
What I Learned Building a Tableau Dashboard for Deloitte’s Data Analytics Simulation
Medium · Data Science
Six Months, 9,541 Restaurant Development Records, and What the Data Actually Says
Medium · Data Science
CRM Analitiği ile Müşteri Değerini Anlamak: RFM, CLTV ve Predictive CLTV Rehberi
Medium · Data Science
Chapters (16)
Intro
0:41
For Loops - The basics
2:08
For Loops - range() function
3:07
For Loops - Iterating over objects
3:53
For Loops - Iterating over a dictionary
6:10
For Loops - enumerate() function
8:01
While Loops - The basics
9:20
While Loops - More complex
10:17
Loop Control Statements - break
11:43
Loop Control Statements - continue
12:29
Loop Control Statements - pass
13:02
Application: Analyzing "The Office" script
14:54
Application: Read the CSV
17:04
Application: Insert Column from CSV in list
19:42
Application: Find "That's what she said"
22:50
Conclusion
🎓
Tutor Explanation
DeepCamp AI