What is DataFrame.iteritems() in pandas?

Overview

The DataFrame.iteritems() method creates an iteration object for DataFrame, which allows it to iterate in all columns of DataFrame. It returns a tuple containing the column name and its content as a series.

It works similarly to the DataFrame.iterrows() method. It displays the title and content of each column.

Syntax


DataFrame.iteritems()

Parameters

It does not take any argument value.

Return value

It yields a tuple containing the column name and content as a pandas series.

Example

#Import Panda module
import pandas as panda
#Data in columns
data = {
"Student_Name": ["Sally", "Anna", "Ryan"],
"Age": [15, 16, 17],
"Class": ["Maths" , "English" , "French"]
}
#Dataframe object
dataframe = panda.DataFrame(data)
#Iteration for columns label and its contents
for Label, Content in dataframe.iteritems():
# column name
print(Label)
# column content
print(Content)

Explanation

  • Line 2: We import the panda module in the program.

  • Lines 4–8: We create a data dictionary with three features and three observations.

  • Line 10: We create a DataFrame on the above-created dictionary.

  • Line 12: We initialize a for loop with variables, Label and Content to get the column title and its contents using the iteritems() method.

  • Lines 14–16: We print DataFrame as the column name and a pandas series.

Free Resources