Pandas DataFrame iterrows() Method
iterrows() method in Pandas is a simple way to iterate over rows of a DataFrame. It returns an iterator that yields each row as a tuple containing the index and the row data (as a Pandas Series). This method is often used in scenarios where row-wise operations or transformations are required. Example:
import pandas as pd
df = pd.DataFrame({'Name': ['Aman', 'Raj'], 'Age': [25, 32]})
# Iterating through the DataFrame
for index, row in df.iterrows():
print(f"Index: {index}, Name: {row['Name']}, Age: {row['Age']}")
Output
Index: 0, Name: Aman, Age: 25 Index: 1, Name: Raj, Age: 32
Explanation: This code uses iterrows() to iterate over each row of the DataFrame and prints the index, Name, and Age for each row.
Syntax
DataFrame.iterrows()
Parameters: It does not take any parameters.
Return Value: It returns an iterator that yields each row as a tuple containing the index and the row data (as a Pandas Series).
Examples of iterrows() Method
Example 1. Iterating Over the First Row of a DataFrame Using iterrows()
Let's understand how to iterate over the rows of DataFrame using iterrows() method of Pandas library. In the below example, we use Pandas DataFrame.iterrows() to iterate over numeric DataFrame rows:
import pandas as pd
# Creating a DataFrame with column names
df = pd.DataFrame(
[[2, 2.5, 100, 4.5, 8.8, 95]],
columns=['int', 'float', 'int', 'float', 'float', 'int']
)
# Get the first row as a Series using iterrows()
itr = next(df.iterrows())[1]
print(itr)
Output:

Explanation: This code creates a DataFrame and uses iterrows() to get the first row as a Series. next(df.iterrows())[1] retrieves and prints the first row of the DataFrame.
Example 2. Calculating Total Sales for Each Product Using iterrows()
In the example, we iterate over the rows involving multiple columns using Pandas DataFrame.iterrows() function.
import pandas as pd
df = pd.DataFrame({
'Product': ['A', 'B', 'C'],
'Price': [10, 20, 15],
'Quantity': [100, 50, 200]
})
# Calculate total sales for each product
for index, row in df.iterrows():
total_sales = row['Price'] * row['Quantity']
print(f"Product {row['Product']} generated ${total_sales} in sales.")
Output
Product A generated $1000 in sales. Product B generated $1000 in sales. Product C generated $3000 in sales.
Explanation: This code iterates through each row of the DataFrame, calculates the total sales (Price * Quantity) for each product, and prints the result.
Example 3. Adding New Columns Using Iteration
We can also, add the new column in this case we'll add 'Category' column using df.at[index, 'Category'] to assign a value for each row based on a condition.
import pandas as pd
df = pd.DataFrame({
'Name': ['Aman', 'Raj', 'Ayush'],
'Age': [25, 32, 37]
})
# Adding a new column based on conditions
for index, row in df.iterrows():
if row['Age'] > 30:
df.at[index, 'Category'] = 'Senior'
else:
df.at[index, 'Category'] = 'Junior'
print(df)
Output
Name Age Category 0 Aman 25 Junior 1 Raj 32 Senior 2 Ayush 37 Senior
Explanation: This code iterates through each row of the DataFrame, checks the Age, and assigns a Category (Senior or Junior) based on the age.
How does iterrows() Method Work?
In simple terms, iterrows() is a generator function. It generates a sequence of (index, row) pairs as you loop through your DataFrame. The index is the row label (or the row number if no custom index is set), and row is a Series object where each value corresponds to a column in that row. Behind the scenes, iterrows() works by iterating over the DataFrame one row at a time:
- Row-by-Row Iteration: It loops over each row in the DataFrame, starting from the first row to the last.
- Index and Row: During each iteration, it returns the index (row label) and the row data as a Series.
- Efficient for Small Datasets: This method is effective when you need to process rows individually, but it’s not the fastest for large datasets.
Related Articles: