Menu

DataFrame.iterrows() Method

Pandas DataFrame iterrows() Method

In this tutorial, we will learn the Python pandas DataFrame.iterrows() method. This method iterates over rows as (index, Series) pairs. When this method applied to the DataFrame, it iterates over the DataFrame rows and returns a tuple which consists of column name and the content as a Series.

The below shows the syntax of the DataFrame.iterrows() method.

Syntax

DataFrame.iterrows()

Example: Iterate over the rows name using the DataFrame.iterrows() Method

When we use the DataFrame.iterrows() method to iterate over the rows of the DataFrame, it will return the generator object. See the below example.

#importing pandas as pd
import pandas as pd
#creating DataFrame
df=pd.DataFrame({"Name":["Navya","Vindya"],"Age":[25,24],"Education":["M.Tech","Ph.d"]},index=['id001', 'id002'])
print("-----------The DataFrame is-------")
print(df)
print("---------Iterate over rows name---------")
print(df.iterrows())

The Output:

-----------The DataFrame is------- Name Age Education id001 Navya 25 M.Tech id002 Vindya 24 Ph.d ---------Iterate over rows name--------- <generator object DataFrame.iterrows at 0x000001E34195B890>

Example: The DataFrame.iterrows() Method

In the last example, we understand that when we use the DataFrame.iterrows() method to iterate over the rows of the DataFrame, it will return the generator object. By using the for loop, we can use this object to generate a pair of index_name and the data. See the below example.

#importing pandas as pd
import pandas as pd
#creating DataFrame
df=pd.DataFrame({"Name":["Navya","Vindya"],"Age":[25,24],"Education":["M.Tech","Ph.d"]},index=['id001', 'id002'])
print("---------Iterate over rows name---------")
for column_name,data in df.iterrows():
    print('index_name:',column_name,'data:',data)

Once we run the program we will get the following output.

---------Iterate over rows name--------- index_name: id001 data: Name Navya Age 25 Education M.Tech Name: id001, dtype: object index_name: id002 data: Name Vindya Age 24 Education Ph.d Name: id002, dtype: object

Example: The DataFrame.iterrows() Method

We can also print a certain row by specifying the index number. See the below example.

#importing pandas as pd
import pandas as pd
#creating DataFrame
df=pd.DataFrame({"Name":["Navya","Vindya"],"Age":[25,24],"Education":["M.Tech","Ph.d"]},index=['id001', 'id002'])
print("---------Iterate over rows name---------")
for column_name,data in df.iterrows():
    print('index_name:',column_name,'data:',data[0])

The Output:

---------Iterate over rows name--------- 

index_name: id001 data: Navya index_name: id002 data: Vindya

Example: The DataFrame.iterrows() Method

We can also specify the name of the column instead of the column name. See the below example.

#importing pandas as pd
import pandas as pd
#creating DataFrame
df=pd.DataFrame({"Name":["Navya","Vindya"],"Age":[25,24],"Education":["M.Tech","Ph.d"]},index=['id001', 'id002'])
print("-----------The DataFrame is-------")
print(df)
print("---------Iterate over rows name---------")
for column_name,data in df.iterrows():
    print('index_name:',column_name,'data:',data['Name'])

The Output:

-----------The DataFrame is------- 

Name Age Education 

id001 Navya 25 M.Tech 

id002 Vindya 24 Ph.d 

---------Iterate over rows name--------- 

index_name: id001 data: Navya index_name: id002 data: Vindya

Conclusion:

In this tutorial, we learned the Python pandas DataFrame.iterrows() method. We learned the syntax and by applying this method on the DataFrame.