Iterate Over Rows in Pandas DataFrame (6 Ways)

Iterating over rows of a Pandas DataFrame is a common operation in various data analysis and processing tasks. Whenever we are working on multiple DataFrame, merging the data from the corresponding rows iteration over the rows can be helpful. Iteration over rows of DataFrame can be helpful also in Data validation where we require to clean data based on some particular conditions and it can be used also in interaction with external APIs or databases where we need to fetch data from an external source on the basis of row-by-row.

In this article, we’ll look at 6 ways to iterate over rows of Pandas DataFrame in Python so that you can use the option that best suits you. Let’s get started.

Methods for Iterating Over Rows of Pandas DataFrame

For Iterating over rows of Pandas DataFrame let’s first create a DataFrame using a CSV file.

Example:

import pandas as pd
df = pd.read_csv('employees.csv')
print(df)

Here we first imported pandas as pd, then created the DataFrame using a CSV file, the name of the CSV file is employees.csv and after that, we printed the DataFrame df.

Output:

Output for printing the DataFrame

There are six methods through which we can iterate over rows of the above Pandas DataFrame which are as follows:

  • Iterating Over Rows Using index Method
  • Iterating Over Rows Using loc[] Method
  • Iterating Over Rows Using iloc[] Method
  • Iterating Over Rows Using iterrows() Method
  • Iterating Over Rows Using itertuples() Method
  • Iterating Over Rows Using the apply() Method

1. Iterating Over Rows Using the index Method

We can use the index method with the for loop to iterate the rows.

Example:

for index in df.index:
    print(df['First Name'][index],
         df['Gender'][index])

Here we have used df.index which provided the indexes. So if we run df.index, it will give us the index starting from 0 and ending at 1000. So here we have written for index in df.index means the for loop starting from 0 and ending at 1000. Then we have written df[‘First Name’][index] and df[‘Gender’][index] to get iterated through every row element from the First Name and Gender column. And in last, we printed all the row elements from the First Name and Gender column.

Output:

Iterating Over the Rows Using index Method

2. Iterating Over Rows Using loc[] Method

We can also use the loc[] method with the for loop to iterate the rows.

Example:

for index in range(len(df)):
    print(df.loc[index,'First Name'],
         df.loc[index,'Gender'],
         df.loc[index,'Start Date'])

Here we have written for index in range(len(df)) which means this for loop will run 1000 times as the length of the data frame df is 1000. In for loop, we have used len(df) to calculate the length of the DataFrame df. Then we have written df.loc[index,’First Name’], df.loc[index,’Gender’] and df.loc[index,’Start Date’] to get iterated through all the row elements from the First Name, Gender, and Start Date columns. Then last, we printed all the row elements from these columns.

Output:

Iterating Over the Rows Using loc[] method

3. Iterating Over Rows Using the iloc[] Method

We can use the iloc[] method with the for loop to iterate the rows. iloc[] is almost similar to loc[], the only difference is we have to use column index here instead of column name.

Example:

for index in range(len(df)):
    print(df.iloc[index,0],
         df.iloc[index,1],
         df.iloc[index,2])

Here also we have used for loop the same as what we have used in the loc[] method. Then we have written df.iloc[index,0], df.iloc[index,1], and df.iloc[index,2] for iteration over all the row elements from the columns First Name which is at index 0, Gender which is at index 1, and Start Date which is at index 2. After that, we printed all the row elements from these columns.

Output:

Iterating Over the Rows Using the  iloc[] method

4. Iterating Over Rows Using iterrows() Method

We can also use the iterrows() method with the for loop to iterate the rows. This is the most convenient and popular way to iterate over rows in a Pandas DataFrame.

Example:

for i,r in df.iterrows():
    print(r['First Name'],
         r['Gender'])

Here we have written for i,r in df.iterrows() which means we have run a for loop for i,r. i stands for index and r stands for rows. Then we have written r[‘First Name’] and r[‘Gender’] to get iterated through all the row elements from the First Name and Gender. Then last, we printed all the row elements from these columns.

Output:

Iterating Over the Rows Using iterrows() Method

5. Iterating Over Rows Using itertuples() Method

Another efficient way to iterate over rows in a Pandas DataFrame is itertuples() method. Let’s see how.

Example:

for row in df.itertuples():
    print(getattr(row,'Gender'),
         getattr(row,'Salary'))

Here we have used df.itertuples( ) which generates an iterator object of the DataFrame. Then we have written getattr(row,’Gender’) and getattr(row,’Salary’) means we have used the getattr() method and in that there are two parameters first is the row which is a counter and the second is the column names through which we have iterated all the row elements from the Gender and Salary. Then last, we printed all the row elements from these columns.

Output:

Iterating Over the Rows Using itertuples() Method

6. Iterating Over Rows Using the apply() Method

We can also use the apply() method with the lambda function to iterate the rows efficiently.

Example:

print(df.apply(lambda row: row['Gender'],axis=1))

Here we have written a statement df.apply(lambda row: row[‘Gender’],axis=1) means we have used the lambda function for iteration and we have also initialized a counter row in it and we have to print the column that’s why we have used axis=1. Then last, we printed all the row elements from the Gender column.

Output:

Iterating Over the Rows Using apply() Method

Summary

Iterating over rows of a Pandas DataFrame can be useful in various scenarios for data analysis, data transformation, and custom calculations. In this tutorial, we have discussed six ways to iterate over rows of Pandas DataFrame with examples. After reading this tutorial, we hope you can easily iterate over rows of a Pandas DataFrame in Python.

Reference

https://stackoverflow.com/questions/72469112/how-to-iterate-through-rows-of-a-dataframe

Priyanshu Singh
Priyanshu Singh
Articles: 44