In Python, pandas.DataFrame.iterrows()
is a method that allows you to loop over a DataFrame row by row. It generates an iterator, returning each index and row data as a pair.
Contents
Example
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]}
df = pd.DataFrame(data)
for index, row in df.iterrows():
print(f"Index: {index}, Name: {row['Name']}, Age: {row['Age']}")
What it does
The iterrows()
method loops through each row of the DataFrame. In each iteration, it returns:
- The index of the row
- A Series object containing the row’s data
Examples
Example 1: Basic usage with a small DataFrame
import pandas as pd
data = {'Fruit': ['Apple', 'Banana', 'Cherry'],
'Quantity': [10, 20, 15]}
df = pd.DataFrame(data)
for index, row in df.iterrows():
print(f"Fruit: {row['Fruit']}, Quantity: {row['Quantity']}")
This loops over each row, printing the fruit and its quantity. It’s simple and direct for accessing each row’s values individually.
Example 2: Using iterrows()
to modify DataFrame content
import pandas as pd
data = {'Price': [5, 10, 7]}
df = pd.DataFrame(data)
for index, row in df.iterrows():
df.at[index, 'Price'] = row['Price'] * 2
print(df)
In this example, iterrows()
is used to update each row’s ‘Price’ by multiplying it by 2. Note: This method works, but using vectorized operations (like df['Price'] *= 2
) is generally faster.
Example 3: Working with mixed data types
import pandas as pd
data = {'Item': ['Book', 'Pen', 'Notebook'],
'Stock': [50, 200, 150],
'InStock': [True, True, False]}
df = pd.DataFrame(data)
for index, row in df.iterrows():
print(f"Item: {row['Item']}, Stock: {row['Stock']}, InStock: {row['InStock']}")
iterrows()
can handle different data types, making it easy to access and print various kinds of information in the DataFrame, such as strings, integers, and booleans.
Example 4: Avoid modifying DataFrame while iterating
import pandas as pd
data = {'A': [1, 2, 3]}
df = pd.DataFrame(data)
for index, row in df.iterrows():
row['A'] = row['A'] * 2 # This will not modify the DataFrame
print(df)
Here, modifying row
within iterrows()
does not change the DataFrame itself. To modify the DataFrame, use df.at[]
or other methods. This shows a key limitation of iterrows()
.