Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

iteritems
dataframe
pandas
communitycreator

What is DataFrame.iteritems() in pandas?

Salman Yousaf

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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)
Using the `DataFrame.iteritems()` method

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.

RELATED TAGS

iteritems
dataframe
pandas
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring