Pandas Tutorial Pandas References

Pandas DataFrame - iterrows() function



The Pandas DataFrame iterrows() function is used to iterate over the DataFrame rows, returning a tuple with the row index and the data of the row as a Series.

Syntax

DataFrame.iterrows()

Parameters

No parameter is required.

Return Value

Yields the following:

  • index : label or tuple of label
    • The index of the row. A tuple for a MultiIndex.
  • data : Series
    • The data of the row as a Series.

Example: iterrows() example

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

import pandas as pd
import numpy as np

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

print(info,"\n")

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

The output of the above code will be:

      Salary  Bonus  Others
2015      25     10       5
2016      24      8       4
2017      30      9       7 

label: 2015
data: 
Salary    25
Bonus     10
Others     5
Name: 2015, dtype: int64

label: 2016
data: 
Salary    24
Bonus      8
Others     4
Name: 2016, dtype: int64

label: 2017
data: 
Salary    30
Bonus      9
Others     7
Name: 2017, dtype: int64

❮ Pandas DataFrame - Functions

5