Itertuples – Python Pandas DataFrame itertuples() Function

Itertuples: Basic iteration over Pandas objects behaves differently depending on the type. It is treated as array-like when iterating over a Series, and basic iteration yields the values. Other data structures, such as DataFrame and Panel, iterate over the keys of the objects in a dict-like fashion.

In a nutshell, basic iteration (for i in object) yields

  • Series: Values
  • DataFrame: Column labels
  • Panel: Item labels

Pandas DataFrame itertuples() Function:

Pandas itertuples example: To iterate over the DataFrame rows as namedtuples, use the itertuples() function Pandas DataFrame.

Syntax:

DataFrame.itertuples(index=True, name='Pandas')

Parameters

index: This is optional. If True, the index is returned as the tuple’s first element. True is the default value.

name: This is optional. It indicates the name for the namedtuples that will be returned, or None to return regular tuples. ‘Pandas’ is the default.

Return Value:

For each row in the DataFrame, returns an object to iterate over namedtuples, with the first field possibly being the index and the following fields being the column values.

Pandas DataFrame itertuples() Function in Python

Example1

Approach:

  • Import pandas module using the import keyword.
  • Import NumPy module using the import keyword.
  • 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.
  • Print the given dataframe
  • Loop in each row of the dataframe using the for loop and itertuples() function and print each row data.
  • The Exit of the Program.

Below is the implementation:

# 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("The given DataFrame:")
print(data_frme)
print()
# Loop in each row of the dataframe using the for loop and 
# itertuples() function and print each row data
for rowdata in data_frme.itertuples():
  print(rowdata)

Output:

The given DataFrame:
  emp_name  emp_age  emp_salary
1     john       25       25000
2     nick       35       40000
3    jessy       38       22000
4     mary       22       80000

Pandas(Index=1, emp_name='john', emp_age=25, emp_salary=25000)
Pandas(Index=2, emp_name='nick', emp_age=35, emp_salary=40000)
Pandas(Index=3, emp_name='jessy', emp_age=38, emp_salary=22000)
Pandas(Index=4, emp_name='mary', emp_age=22, emp_salary=80000)

Example2

Approach:

  • Import pandas module using the import keyword.
  • 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.
  • Print the given dataframe
  • Pass the index=False as an argument to the itertuples() function that removes index as the first element in the result.
  • Pass the name=’Btechgeeks’ as argument to the itertuples() function that gives user-specified name in the result instead of pandas.
  • Pass the name=None as an argument to the itertuples() function that removes names in the result.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# 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("The given DataFrame:")
print(data_frme)
print()

# Pass the index=False as an argument to the itertuples() function that
# removes index as first element in the result
for row_val in data_frme.itertuples(index=False):
  print(row_val)
print()

# Pass the name='Btechgeeks' as argument to the itertuples() function that
# gives user specified name in the result instead of pandas
for row_val in data_frme.itertuples(name='Btechgeeks'):
  print(row_val)
print()

# Pass the name=None as an argument to the itertuples() function that
# removes names in the result.
for row_val in data_frme.itertuples(name=None):
  print(row_val)

Output:

The given DataFrame:
  emp_name  emp_age  emp_salary
1     john       25       25000
2     nick       35       40000
3    jessy       38       22000
4     mary       22       80000

Pandas(emp_name='john', emp_age=25, emp_salary=25000)
Pandas(emp_name='nick', emp_age=35, emp_salary=40000)
Pandas(emp_name='jessy', emp_age=38, emp_salary=22000)
Pandas(emp_name='mary', emp_age=22, emp_salary=80000)

Btechgeeks(Index=1, emp_name='john', emp_age=25, emp_salary=25000)
Btechgeeks(Index=2, emp_name='nick', emp_age=35, emp_salary=40000)
Btechgeeks(Index=3, emp_name='jessy', emp_age=38, emp_salary=22000)
Btechgeeks(Index=4, emp_name='mary', emp_age=22, emp_salary=80000)

(1, 'john', 25, 25000)
(2, 'nick', 35, 40000)
(3, 'jessy', 38, 22000)
(4, 'mary', 22, 80000)

 

Leave a Comment