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.
DataFrame.iteritems()
It does not take any argument value.
It yields a tuple containing the column name and content as a pandas series.
#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)
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.