Pandas dataframe iterrows – Python Pandas DataFrame iterrows() Function

Pandas DataFrame iterrows() Function:

Pandas dataframe iterrows: Iterate through the DataFrame rows with the iterrows() function of the Pandas DataFrame, which returns a tuple with the row index and the row data as a Series.

Syntax:

DataFrame.iterrows()

Parameters: This method doesn’t accept any parameters

Return Value:

Gives the following:

index: label or a tuple of label

This is required. The row’s index number. A tuple for a MultiIndex.

data: Series

This is required. It is the data of the row as a Series.

it: generator

This is required. A generator that loops across the rows of the frame.

Pandas DataFrame iterrows() Function in Python

Approach:

Below is the implementation:

Output:

# Import pandas module using the import keyword.
import pandas as pd
# Import NumPy module using the import keyword.
import numpy as np
# Pass some random key-value pair(dictionary), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "emp_name": ["john", "nick" , "jessy", "mary"],
  "emp_age": [25, 35, 38, 22],
  "emp_salary": [25000, 40000, 22000, 80000]},
  index= [1, 2, 3, 4]
)

print("The given DataFrame:")
print(data_frme)
print()

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