Pandas DataFrame items() Method

In this tutorial, we will learn the Python pandas DataFrame.items() method. This method iterates over (column name, Series) pairs. When this method applied to the DataFrame, it iterates over the DataFrame columns and returns a tuple which consists of column name and the content as a Series.

The below is the syntax of the DataFrame.items() method.

Syntax

DataFrame.items()

Example 1: Iterate DataFrame using the DataFrame.items() Method

When we use the DataFrame.items() method to iterate over the columns of the DataFrame, it will return the generator object. See the below example.

#importing pandas as pd
import pandas as pd
#creating DataFrame
df=pd.DataFrame({"Name":["Navya","Vindya"],"Age":[25,24],"Education":["M.Tech","Ph.d"]},index=['id001', 'id002'])
print("-----------The DataFrame is-------")
print(df)
print("---------Iterate over column name---------")
print(df.items())


-----------The DataFrame is-------
Name Age Education
id001 Navya 25 M.Tech
id002 Vindya 24 Ph.d
---------Iterate over column name---------
<generator object DataFrame.items at 0x000001E341897350>

Example 2: Iterate DataFrame using the DataFrame.items() Method

In the last example, we understand that when we use the DataFrame.items() method to iterate over the columns of the DataFrame, it will return the generator object. By using the for loop, we can use this object to generate a pair of column_name and values. See the below example.

#importing pandas as pd
import pandas as pd
#creating DataFrame
df=pd.DataFrame({"Name":["Navya","Vindya"],"Age":[25,24],"Education":["M.Tech","Ph.d"]},index=['id001', 'id002'])
print("---------Iterate over column name---------")
for column_name,data in df.items():
    print('column_name:',column_name,'data:',data)


---------Iterate over column name---------
column_name: Name data: id001 Navya
id002 Vindya
Name: Name, dtype: object
column_name: Age data: id001 25
id002 24
Name: Age, dtype: int64
column_name: Education data: id001 M.Tech
id002 Ph.d
Name: Education, dtype: object

Example 3: Iterate DataFrame using the DataFrame.items() Method

We can also print a certain row by specifying the index number. See the below example.

#importing pandas as pd
import pandas as pd
#creating DataFrame
df=pd.DataFrame({"Name":["Navya","Vindya"],"Age":[25,24],"Education":["M.Tech","Ph.d"]},index=['id001', 'id002'])
print("-----------The DataFrame is-------")
print(df)
print("---------Iterate over column name---------")
for column_name,data in df.items():
    print('column_name:',column_name,'data:',data[0])


-----------The DataFrame is-------
Name Age Education
id001 Navya 25 M.Tech
id002 Vindya 24 Ph.d
---------Iterate over column name---------
column_name: Name data: Navya
column_name: Age data: 25
column_name: Education data: M.Tech

Example 4: Iterate DataFrame using the DataFrame.items() Method

We can iterate specified index or column apart from the whole dataframe. See the below example.

#importing pandas as pd
import pandas as pd
#creating DataFrame
df=pd.DataFrame({"Name":["Navya","Vindya"],"Age":[25,24],"Education":["M.Tech","Ph.d"]},index=['id001', 'id002'])
print("-----------The DataFrame is-------")
print(df)
print("---------Iterate over column name---------")
for column_name,data in df.items():
    print('column_name:',column_name,'data:',data['id001'])


-----------The DataFrame is-------
Name Age Education
id001 Navya 25 M.Tech
id002 Vindya 24 Ph.d
---------Iterate over column name---------
column_name: Name data: Navya
column_name: Age data: 25
column_name: Education data: M.Tech

Conclusion:

In this tutorial, we learned the Python pandas DataFrame.items() method. We learned the syntax and applying this method on the DataFrame to understand the DataFrame.items() method.