What is Dataframe.items() method in pandas?

Overview

DataFrame.items() create an iteration object for DataFrame, which allows it to iterate each column of the DataFrame. This method works similarly to the DataFrame.iteritems() method, which displays each column's name and content as a Series.

Syntax

The syntax for using this method is as follows:

Dataframe.items()

Parameters

The DataFrame.items() does not take any parameters.

Return value

It returns a tuple that contains:

  • label: The column name.
  • content: The column entries belonging to each label, as a pandas Series object.

Code example

In the code snippet, we'll create a DataFrame and then invoke Dataframe.items() to get column names and data separately:

#Import Panda module
import pandas as panda
# data as dictionary
record = {
"Employee_Name": ["Jenifer", "Amenda", "Yuonne"],
"Department": ["Finance", "Fintech", "Software"],
"Designation":["Assistant Manager","Employee","Project Manager"],
"Salary": [12000 , 87000,45000]
}
# creating Dataframe object
dataframe = panda.DataFrame(record)
# iteration for columns label and its contents
for Label, Content in dataframe.items():
# Print data as label and series
print(Label)
print(Content)

Code Explanation

  • Line 2: We import the panda module.
  • Lines 5 to 10: We have data as a Python dictionary.
  • Line 13: We create a DataFrame object to assign data to it.
  • Line 16: We initialize a for loop with variables Label and Content to get the column title and its contents using the items() method.
  • Lines 18 to 19: We print the accessed values.

Free Resources