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 pandasSeriesobject.
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 moduleimport pandas as panda# data as dictionaryrecord = {"Employee_Name": ["Jenifer", "Amenda", "Yuonne"],"Department": ["Finance", "Fintech", "Software"],"Designation":["Assistant Manager","Employee","Project Manager"],"Salary": [12000 , 87000,45000]}# creating Dataframe objectdataframe = panda.DataFrame(record)# iteration for columns label and its contentsfor Label, Content in dataframe.items():# Print data as label and seriesprint(Label)print(Content)
Code Explanation
- Line 2: We import the
pandamodule. - Lines 5 to 10: We have data as a Python dictionary.
- Line 13: We create a
DataFrameobject to assign data to it. - Line 16: We initialize a
forloop with variablesLabelandContentto get the column title and its contents using theitems()method. - Lines 18 to 19: We print the accessed values.