Pandas DataFrame.itertuples
Pandas DataFrame.itertuples
The DataFrame.itertuples
method in pandas is used to iterate over rows of a DataFrame as named tuples. This is a fast and memory-efficient way to access row data, particularly for large DataFrames.
Syntax
The syntax for DataFrame.itertuples
is:
DataFrame.itertuples(index=True, name='Pandas')
Here, DataFrame
refers to the pandas DataFrame whose rows are being iterated.
Parameters
Parameter | Description |
---|---|
index | If True , includes the index as the first element of the tuple. Defaults to True . |
name | Specifies the name of the returned named tuple type. If None , regular tuples are returned instead of named tuples. Defaults to 'Pandas' . |
Returns
An iterator yielding named tuples of row data. Each tuple contains the index (if index=True
) and the values of the row.
Examples
Iterating Over Rows with Named Tuples
Use itertuples
to iterate over rows and access their data as named tuples.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Priya'],
'Age': [25, 30, 35],
'Salary': [70000, 80000, 90000]
}
df = pd.DataFrame(data)
# Iterate over rows as named tuples
print("Iterating with Named Tuples:")
for row in df.itertuples():
print(row)
Output
Iterating with Named Tuples:
Pandas(Index=0, Name='Arjun', Age=25, Salary=70000)
Pandas(Index=1, Name='Ram', Age=30, Salary=80000)
Pandas(Index=2, Name='Priya', Age=35, Salary=90000)
Accessing Specific Columns in Named Tuples
Access specific columns in each row using attribute-style access on named tuples.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Priya'],
'Age': [25, 30, 35],
'Salary': [70000, 80000, 90000]
}
df = pd.DataFrame(data)
# Access specific columns
print("Accessing Specific Columns:")
for row in df.itertuples():
print(f"Name: {row.Name}, Age: {row.Age}")
Output
Accessing Specific Columns:
Name: Arjun, Age: 25
Name: Ram, Age: 30
Name: Priya, Age: 35
Excluding the Index
Set index=False
to exclude the index from the tuples.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Priya'],
'Age': [25, 30, 35],
'Salary': [70000, 80000, 90000]
}
df = pd.DataFrame(data)
# Iterate over rows without including the index
print("Iterating Without Index:")
for row in df.itertuples(index=False):
print(row)
Output
Iterating Without Index:
Pandas(Name='Arjun', Age=25, Salary=70000)
Pandas(Name='Ram', Age=30, Salary=80000)
Pandas(Name='Priya', Age=35, Salary=90000)
Returning Regular Tuples
Set name=None
to return regular tuples instead of named tuples.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Priya'],
'Age': [25, 30, 35],
'Salary': [70000, 80000, 90000]
}
df = pd.DataFrame(data)
# Return regular tuples
print("Iterating with Regular Tuples:")
for row in df.itertuples(name=None):
print(row)
Output
Iterating with Regular Tuples:
(0, 'Arjun', 25, 70000)
(1, 'Ram', 30, 80000)
(2, 'Priya', 35, 90000)
Summary
In this tutorial, we explored the DataFrame.itertuples
method in pandas. Key takeaways include:
- Using
itertuples
to iterate over rows as named tuples. - Accessing specific columns using attribute-style access.
- Excluding the index or returning regular tuples for flexibility.
The DataFrame.itertuples
method is a fast and efficient way to iterate through row data in pandas.