Signup/Sign In

Pandas DataFrame iteritems() Method

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

The below is the syntax of the DataFrame.iteritems() method.

Syntax

DataFrame.iteritems()

Yields

label: It represents the object that is the name of the column in the DataFrame being iterated over.

content: It represents the Series. Each label's content represents a Series.

Example: Iterate over the column name using the DataFrame.iteritems() method

When we use the DataFrame.items() method to iterate over the columns 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 column name---------")
print(df.iteritems())


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

Example: using for loop to iterate over the column name using the DataFrame.iteritems() method

In the last example we understand that when we use the DataFrame.iteritems() method to iterate over the columns of the DataFrame, it will return the generator object. By using the for loop, we can use this object to generate a pair of column_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 column name---------")
for column_name,data in df.iteritems():
    print('column_name:',column_name,'data:',data)


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

Example: Using for loop to iterate over the specified index using the DataFrame.iteritems() 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("-----------The DataFrame is-------")
print(df)
print("---------Iterate over column name---------")
for column_name,data in df.iteritems():
    print('column_name:',column_name,'data:',data[0])


-----------The DataFrame is-------
Name Age Education
id001 Navya 25 M.Tech
id002 Vindya 24 Ph.d
---------Iterate over column name---------
column_name: Name data: Navya
column_name: Age data: 25
column_name: Education data: M.Tech

Example: Using for loop to iterate over the specified index using the DataFrame.iteritems() method

We can also specify the name of the index instead of 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("-----------The DataFrame is-------")
print(df)
print("---------Iterate over column name---------")
for column_name,data in df.iteritems():
    print('column_name:',column_name,'data:',data['id001'])


-----------The DataFrame is-------
Name Age Education
id001 Navya 25 M.Tech
id002 Vindya 24 Ph.d
---------Iterate over column name---------
column_name: Name data: Navya
column_name: Age data: 25
column_name: Education data: M.Tech

Conclusion:

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



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.