Pandas Tutorial Pandas References

Pandas DataFrame - iteritems() function



The Pandas DataFrame iteritems() function is used to iterate over the DataFrame columns, returning a tuple with the column name and the content as a Series.

Syntax

DataFrame.iteritems()

Parameters

No parameter is required.

Return Value

Yields the following:

  • label : object
    • The column names for the DataFrame being iterated over.
  • content : Series
    • The column entries belonging to each label, as a Series.

Example: iteritems() example

The example below demonstrates the usage of iteritems() function.

import pandas as pd
import numpy as np

info = pd.DataFrame({
  "Salary": [25, 24, 30, 28, 25],
  "Bonus": [10, 8, 9, np.nan, 9],
  "Others": [5, 4, 7, 5, 8]},
  index= ["2015", "2016", "2017", "2018", "2019"]
)

print(info,"\n")

for label, content in info.iteritems():
  print(f'label: {label}')
  print(f'content: \n{content}')
  print()

The output of the above code will be:

      Salary  Bonus  Others
2015      25   10.0       5
2016      24    8.0       4
2017      30    9.0       7
2018      28    NaN       5
2019      25    9.0       8 

label: Salary
content: 
2015    25
2016    24
2017    30
2018    28
2019    25
Name: Salary, dtype: int64

label: Bonus
content: 
2015    10.0
2016     8.0
2017     9.0
2018     NaN
2019     9.0
Name: Bonus, dtype: float64

label: Others
content: 
2015    5
2016    4
2017    7
2018    5
2019    8
Name: Others, dtype: int64

❮ Pandas DataFrame - Functions

5