Pandas iterrows() – Iterate over DataFrame Rows

DataFrame are organized data in the form of rows and columns. It is used in modern data analysis to perform different data operations such as storing, sorting, merging, etc. Python supports different packages that are used to perform these operations. Some of the packages are NumPy, pandas, scipy, etc. In this write-up, we will provide a complete guide on the panda “iterrows()” function with examples. The following aspects are discussed in this blog post: 

So let’s get started!

What is Pandas “iterrows()” Function?

The Pandas “iterrows()” function is used to iterate over the rows of DataFrame. The syntax of the “iterrows()” function is shown below:

DataFrame.iterrows()

The “iterrows()” function yield the following output:

  • This function returns the Index of the row as output.
  • This function also returns the Data of the row.

Let’s understand the concept of the “iterrows()” function via the following example:

Example: Using Pandas “iterrows()” Function to Iterate over DataFrame Rows

In the example given below, the “iterrows()” function of pandas library iterates over the rows of user-defined DataFrame.

Code:

import pandas as pd

#Creating DataFrame
data_value = pd.DataFrame({
    'name': ['john', 'david', 'joseph', 'lily'],
    'marks': [48, 44, 47, 48]})

#iterate through each row of dataframe
for index, row in data_value.iterrows():
    print('Row no',index, ': ', row['name'], 'got', row['marks'], 'marks')

In the above code:

  • The “pandas” DataFrame library is imported at the start of the program.
  • The DataFrame value is initialized in a dictionary variable named “data_value” and created with the help of a function named “pd.DataFrame()”.
  • The “for loop” iterates over the rows of DataFrame using the “iterrows()” function.
  • To print the iterated rows one by one, including their index, three parameters are passed inside the parenthesis. The first parameter “index” prints the value of the row “index”, and the second parameter “row[‘name’]” prints the value of elements placed in the “name” columns. Similarly, the third parameter, “row[‘marks’]” prints the value of elements placed in “marks” columns.

Output:

The output shows the iteration of DataFrame.

That’s all from this Python guide.

Conclusion

In Python, the pandas “iterrows()” function iterates over the DataFrame Rows and performs user-defined operations. The panda “iterrows()” function yields index and row content as an output. The “iterrows()” function is not the preferred use because it can change the type of our data like every series is converted into an object. This Python guide has explained all the details of the panda “iterrows()” function.