Pandas DataFrame Itertuples Method

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will learn about the Python Pandas DataFrame.iteruples() method. The iteruples() method in Pandas iterates over the rows of a DataFrame and returns each row as a named tuple. It is a useful method for iterating over and working with the rows of a DataFrame.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) pandas(("`Pandas`")) -.-> pandas/DataSelectionGroup(["`Data Selection`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") pandas/DataSelectionGroup -.-> pandas/select_columns("`Select Columns`") pandas/DataSelectionGroup -.-> pandas/conditional_selection("`Conditional Selection`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-68644{{"`Pandas DataFrame Itertuples Method`"}} pandas/select_columns -.-> lab-68644{{"`Pandas DataFrame Itertuples Method`"}} pandas/conditional_selection -.-> lab-68644{{"`Pandas DataFrame Itertuples Method`"}} python/booleans -.-> lab-68644{{"`Pandas DataFrame Itertuples Method`"}} python/for_loops -.-> lab-68644{{"`Pandas DataFrame Itertuples Method`"}} python/lists -.-> lab-68644{{"`Pandas DataFrame Itertuples Method`"}} python/tuples -.-> lab-68644{{"`Pandas DataFrame Itertuples Method`"}} python/dictionaries -.-> lab-68644{{"`Pandas DataFrame Itertuples Method`"}} python/importing_modules -.-> lab-68644{{"`Pandas DataFrame Itertuples Method`"}} python/numerical_computing -.-> lab-68644{{"`Pandas DataFrame Itertuples Method`"}} python/data_analysis -.-> lab-68644{{"`Pandas DataFrame Itertuples Method`"}} python/build_in_functions -.-> lab-68644{{"`Pandas DataFrame Itertuples Method`"}} end

Understand the Syntax

The syntax of the iteruples() method is as follows:

DataFrame.iteruples(index=True, name='Pandas')
  • index: It is a boolean value indicating whether to include the index as the first element of the tuple. The default value is True.
  • name: It is a string value representing the name of the returned named tuples. The default value is "Pandas". If set to None, regular tuples are returned instead of named tuples.

Iterate Over DataFrame Rows

In this step, we will create a DataFrame and use the iteruples() method to iterate over its rows. The method returns a map object, which can be used in a loop to iterate over each row as a named tuple.

## import pandas
import pandas as pd

## create DataFrame
df=pd.DataFrame({"Name":["Navya","Vindya"],"Age":[25,24],"Education":["M.Tech","Ph.d"]},index=['index_1', 'index_2'])

## iterate over DataFrame rows
for row in df.iteruples():
    print(row)

Control Index and Tuple Naming

In this step, we will explore how to control the index and naming of the returned tuples. By default, the index is included as the first element of the tuple, and the tuples are named "Pandas". We can modify these settings by passing arguments to the iteruples() method.

## import pandas
import pandas as pd

## create DataFrame
df=pd.DataFrame({"Name":["Navya","Vindya"],"Age":[25,24],"Education":["M.Tech","Ph.d"]},index=['index_1', 'index_2'])

## iterate over DataFrame rows without index and custom name
for row in df.iteruples(index=False, name='Rows'):
    print(row)

Summary

In this lab, we learned about the Pandas DataFrame.iteruples() method, which is useful for iterating over the rows of a DataFrame and accessing the values of each row. We saw how to use the method, control the inclusion of the index, and customize the naming of the returned tuples. This method can be helpful in various data analysis and manipulation tasks with Pandas DataFrames.