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 moduleimport pandas as panda#Data in columnsdata = {"Student_Name": ["Sally", "Anna", "Ryan"],"Age": [15, 16, 17],"Class": ["Maths" , "English" , "French"]}#Dataframe objectdataframe = panda.DataFrame(data)#Iteration for columns label and its contentsfor Label, Content in dataframe.iteritems():# column nameprint(Label)# column contentprint(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
forloop with variables,LabelandContentto get the column title and its contents using theiteritems()method.Lines 14–16: We print DataFrame as the column name and a pandas series.