In this lesson,
I want to show you how you can use loops with pandas dataframes and how to
iterate over a pandas data frame. So here, I've got a simple dictionary,
I've got two keys, student and score, and under student
I've got a list of student names, and under score
I've got a list of their corresponding scores.
Now we know that we can loop through a dictionary very simply by creating a for
loop and then we say, well,
we're going to go through each of the key and values inside this student
dictionary.
And then we're going to get all of the items in order to be able to loop through
it. So now when I print each of the keys,
you can see that it goes through the dictionary and prints both of the keys.
And similarly, I can get it to loop through both of the values.
So this is how we've been looping through dictionaries
and we've been using it in our dictionary comprehension.
Now you can loop through a data frame in the same way that you loop through a
dictionary. In a lot of ways,
you can consider a data frame pretty much as if you're working with a Python
dictionary. So I'm going to go ahead and import pandas
and I'm going to use pandas to create a new data frame,
and it's going to be created from our student dictionary.
So you've seen all of this before,
and I'll just call this the student_data_frame and I can print it for you to see
what it looks like.
Okay.
This is our data frame.
It looks like a pretty standard table with the first column being all of the
indices. So at zero index is this first row,
and that basically denotes the index of each row.
Now working with this data frame,
we can actually loop through a data frame using the same method as before.
So we can say for key, value in our student_data_frame .items.
So if I print each of the keys,
you can see it's just going to give me the titles of each column.
But if I print each of the values,
then it's going to give me the data in each of the columns.
Now this is not particularly useful because it's basically just looping through
the names of our columns and then the data inside each column.
This is why pandas has a inbuilt loop
and it's a method called iterrows.
And it allows us to loop through each of the rows of the data frame rather than
each of the columns.
And the way that we do that is we again use a for loop and then we can get hold
of each of the index inside each row,
so that corresponds to the number in that first column.
And then we can get hold of the data in the row.
And then we can say for index row in data frame,
which is student_data_frame, and then its that method.iter
rows.
And now I can loop through each of those rows and print out either the index for
each of those rows.
So you can see that this is going to print out our data frame here,
And then in order to print out each of the index at 0, 1, 2.
But I can also print out each of the rows.
So now I get the first row has a student and a score,
the second row has a student and a score, and the third row has a student and
score.
So each of these rows is a pandas series object. So that means we can tap into the
row and then get hold of the value under a particular column by using the dot
notation. So we can say row.student
and now when it goes through the loop,
you can see first, it's going to print out our entire data frame,
and then it's going to print out each of the students inside that data frame.
Now I can also say row.score,
and now it's going to give me each of the scores inside the data frame.
And I can even do something like this where I say
if the row.student is equal to Angela,
well then we can print that particular row that we're currently looping on,
.score. And this way we would get the student, Angela's score
which happens to be 56, as you can verify here.