Different Ways to Iterate Over Rows in Pandas DataFrame | GeeksforGeeks

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone welcome to geeksforgeeks in this video we are going to know how can we iterate over the rows there are various methods to iterate over the rows such as using index method using lock method using ilock method and there are several other methods too and we are going to see them one by one so let's first of all import pandas as pd port pandas as pd let's run it so my pandas is imported now i am going to create my data frame and i am going to use the employees dataset csv file so df is equal to pd dot read csv and provide the name of that csv file and let's see the head part let's see what are the columns in it so i can see that there is a name column gender start date last login time then salary and several other methods several other columns are also there right now i'm going to iterate over the rows so first method is let me put the first topic that is using index method and in this we are going to use a for loop for iterating over the rows so for index in df dot index so as we know that df dot index provides me the indexes let's first run that only df dot index and if i run it i can see that it tells that it starts from zero it ends at thousand right so it means this loop is also going to start from zero and end at thousand perfect let's remove this so for index in df dot index and colon what i want to print print df at column name so copy this paste it over here at index right because i want to print every name that means my for loop is going to start from 000 000 so every element of this column that means first name will be printed why and how because this for loop is going to run from 0 and at thousand so every element present at that particular index will be printed so let's see what happens so i can see that every name i'm getting right now with this name i also want their gender right so let's put a comma and df at gender add index so if i run it i can see that name and gender is getting printed so this is how i'm iterating over every row element right so this was using the index method with a follow now let's put the second topic that is using lock method in this we are going to use for loop only but with that lock method so let's use for index in range length of df that means this for loop is going to run 1000 times because the length of this data frame is thousand so let's put a colon i want to print df dot log then square brackets put that index and comma and the name of the column so name of the column is first name right with this i also want to print their gender so again df dot lock square brackets index comma gender if i run it i can see the same result so these were the two methods that is using df.index and df.lock they are just printing the row elements of the columns that i have provided here so first name and gender will be printed let's take one more column so let's copy this start date and we will print df.lock square brackets index comma paste it let's run it i can see that all the row elements of these three columns are getting printed using the lock method now let's see the third method that is using ilock that is index lock method syntax will be almost similar so for index in range length of df would be colon and we have to reprint the same thing just we have to write df dot i log square brackets then index and instead of that column name you have to write the index of that column so if i come over here i can see that first name i want to print and first name is present at zero index right so let's print zero and df dot i lock index square brackets index comma the second thing is male or female and that is presented index one so one and if i run it i can see the same result now let's say i want to print the start date also so i will copy this paste it over here and instead of 1 i will write 2. let's run it i can see the 3 column elements are there perfect now using the fourth method fourth method is iter rows function so in this we have to run a for loop for i comma r so we are getting two counters in pf dot iter rows function then this colon and we are going to print r at first name i am going to tell you what is happening and i am going to print r at gender if i run it i can see the same result so this r is nothing but the row so i that is index comma r that is row right so row at first name that means that means row element of first name row element of gender you have to print and this will run unless and until we reach to the end of the data frame so this is how we can use iter rows for iterating over the row elements now we have to see the fifth method either tuples so in this also we have to use a for loop so for row in df dot either tuples and we have to print and over here we have to use a keyword and that keyword is get attributes in which as a parameter we have to provide this row which is a counter right and comma you have to provide the column name so my column name let's take gender this time and let's say we print the second column elements also so we have to write one more time you get a dtr provide the counter then you have to provide the column name so let's take the column name as let's take something different this time let's take the salary so let's paste it over here and we can print it and we can say and we can see that the gender and the salary is printed so this was using the tuples method now we have to see the last method that is using apply method so let's put the topic sixth method using apply function so in this we don't have to use a for loop we just need to print a statement so print and we are going to use a lambda functions over here so first of all df dot apply that means we have to apply this lambda function and which is that lambda function i am going to type lambda we have to initialize a counter that will be called row and we want to print the row elements of this gender column so let's print it and we also have to provide the axis because this gender is a column right that's why access will be 1. now if i'm going to run it i can see that all the row elements of gender column are getting printed so this was how you can iterate over the row elements of a particular column so these were the six methods and if you want to give us any suggestions or feedback you can do that in the comment section thank you for watching this video [Music]
Info
Channel: GeeksforGeeks
Views: 40,886
Rating: undefined out of 5
Keywords: GeeksforGeeks, iteration over rows pandas, dataframe row iteration, iterating over rows in dataframe python, how to iterate over rows in pandas, different ways to iterate over rows in pandas dataframe, row iteration in pandas, iterrows pandas dataframe, how pandas dataframe works, what is pandas dataframe, itertuples pandas example, apply method pandas dataframe, pandas dataframe, python pandas dataframe, iterating over rows in pandas
Id: mT-2AxZLtvw
Channel Id: undefined
Length: 9min 26sec (566 seconds)
Published: Tue Jul 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.