Recently, I was working on a data analysis project for a US retail chain where I needed to apply conditional updates to thousands of product prices based on their categories and current market trends. The challenge was finding an efficient way to iterate through each row and make specific updates based on multiple conditions.
That’s when Panda’s iterrows() method proved to be incredibly useful. While there are several ways to update values in a DataFrame, iterrows() provides a flexible approach when you need row-by-row processing with complex logic.
In this article, I’ll show you multiple ways to use the Pandas iterrows() method to update values in a DataFrame, complete with practical examples.
Pandas iterrows()
The iterrows() method in Pandas is an iterator that yields each row in a DataFrame as a tuple containing the index and the row data as a Series. This makes it particularly useful when you need to:
- Apply complex conditional logic to update values
- Process each row individually based on multiple criteria
- Perform calculations that depend on values from other rows
Let’s start with a simple example:
import pandas as pd
# Creating a sample DataFrame with US sales data
data = {
'Product': ['Laptop', 'Smartphone', 'Tablet', 'Headphones', 'Smartwatch'],
'Price': [1200, 800, 350, 200, 250],
'State': ['California', 'New York', 'Texas', 'Florida', 'Illinois'],
'Units_Sold': [50, 120, 75, 90, 60]
}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)Now, let’s explore different approaches to update values using iterrows().
Update Values Using Pandas iterrows() Method in Python
Now, I will explain methods to update values using the Pandas iterrows() method in Python.
Read Python Pandas Write to Excel
Method 1: Basic Update Using iterrows()
The easiest use of iterrows() in Python is to iterate through each row and update values based on a condition:
# Applying 10% discount to products priced over $300
for index, row in df.iterrows():
if row['Price'] > 300:
df.at[index, 'Price'] = row['Price'] * 0.9
print("\nDataFrame after 10% discount on expensive items:")
print(df)Output:
DataFrame after 10% discount on expensive items:
Product Price State Units_Sold
0 Laptop 1080 California 50
1 Smartphone 720 New York 120
2 Tablet 315 Texas 75
3 Headphones 200 Florida 90
4 Smartwatch 250 Illinois 60I executed the above example code and added the screenshot below.

In this example, I’m iterating through each row, checking if the product price is over $300, and applying a 10% discount if the condition is met. The df.at[index, 'Price'] is used for updating the specific cell.
Method 2: Create a New Column Using iterrows()
Another common use case is to create a new column based on calculations from existing columns:
# Adding a Revenue column calculated from Price and Units_Sold
df['Revenue'] = 0 # Initialize new column
for index, row in df.iterrows():
df.at[index, 'Revenue'] = row['Price'] * row['Units_Sold']
print("\nDataFrame with Revenue column:")
print(df)Output:
DataFrame with Revenue column:
Product Price State Units_Sold Revenue
0 Laptop 1200 California 50 60000
1 Smartphone 800 New York 120 96000
2 Tablet 350 Texas 75 26250
3 Headphones 200 Florida 90 18000
4 Smartwatch 250 Illinois 60 15000I executed the above example code and added the screenshot below.

This example shows how to create a Revenue column by multiplying the Price and Units_Sold values for each row.
Check out Create Plots Using Pandas crosstab() in Python
Method 3: Conditional Updates with Multiple Criteria
One of the strengths of iterrows() method in Python is handling complex conditional logic:
# Applying regional price adjustments based on state and current price
for index, row in df.iterrows():
if row['State'] == 'California' and row['Price'] > 500:
df.at[index, 'Price'] = row['Price'] * 1.05 # 5% price increase due to high demand
elif row['State'] == 'Texas':
df.at[index, 'Price'] = row['Price'] * 0.92 # 8% discount for Texas market
elif row['Units_Sold'] < 70:
df.at[index, 'Price'] = row['Price'] * 0.95 # 5% discount for slower-selling items
print("\nDataFrame after regional price adjustments:")
print(df)Output:
DataFrame after regional price adjustments:
Product Price State Units_Sold
0 Laptop 1260.0 California 50
1 Smartphone 800.0 New York 120
2 Tablet 322.0 Texas 75
3 Headphones 200.0 Florida 90
4 Smartwatch 237.5 Illinois 60I executed the above example code and added the screenshot below.

This example demonstrates how we can apply different price adjustments based on multiple criteria like state, current price, and sales performance.
Read np.where in Pandas Python
Method 4: Use iterrows() with apply() for Better Performance
While iterrows() is convenient, it can be slow for large DataFrames. We can combine it with apply() for better performance:
# Define a function to calculate a custom discount
def calculate_discount(row):
base_discount = 0
# Loyalty discount based on units sold
if row['Units_Sold'] > 100:
base_discount += 0.05
# Regional discount
if row['State'] in ['Florida', 'Illinois']:
base_discount += 0.03
# Calculate final price
return row['Price'] * (1 - base_discount)
# Apply the function to each row
df['Discounted_Price'] = df.apply(calculate_discount, axis=1)
print("\nDataFrame with custom discount calculation:")
print(df)This method defines a function that applies complex discount logic and then uses the apply() method to execute it for each row, which is generally faster than pure iterrows() for large datasets.
Performance Considerations When Using iterrows()
When working with Pandas, it’s important to know that iterrows() isn’t always the most efficient approach. Here I have given some performance considerations:
- For simple operations, vectorized operations are much faster
- For large DataFrames (100,000+ rows), consider alternatives like:
- apply()
- vectorized operations
- numpy operations
Here’s a quick comparison:
import time
# Creating a larger DataFrame
large_df = pd.DataFrame({
'A': range(100000),
'B': range(100000)
})
# Method 1: Using iterrows() (slower)
start_time = time.time()
for index, row in large_df.iterrows():
large_df.at[index, 'C'] = row['A'] + row['B']
print(f"iterrows() time: {time.time() - start_time:.4f} seconds")
# Reset DataFrame
large_df = pd.DataFrame({
'A': range(100000),
'B': range(100000)
})
# Method 2: Vectorized operation (faster)
start_time = time.time()
large_df['C'] = large_df['A'] + large_df['B']
print(f"Vectorized operation time: {time.time() - start_time:.4f} seconds")Real-World Example: Update Sales Data for a Retail Chain
Let’s look at a more comprehensive real-world example where we need to update a retail chain’s product database:
# More realistic retail dataset
retail_data = {
'Product_ID': ['P001', 'P002', 'P003', 'P004', 'P005', 'P006'],
'Product_Name': ['Premium Laptop', 'Budget Tablet', 'Wireless Headphones',
'Smart TV', 'Gaming Console', 'Fitness Tracker'],
'Category': ['Electronics', 'Electronics', 'Audio',
'Electronics', 'Gaming', 'Wearables'],
'Base_Price': [1299.99, 249.99, 179.99, 899.99, 499.99, 129.99],
'Stock': [25, 50, 100, 15, 30, 75],
'Supplier': ['TechCorp', 'TabletInc', 'AudioPro',
'ScreenMakers', 'GameWorld', 'FitTech']
}
retail_df = pd.DataFrame(retail_data)
# Let's update prices based on multiple business rules
for index, row in retail_df.iterrows():
# Apply category-based pricing adjustments
if row['Category'] == 'Electronics':
# Electronics have a seasonal 5% markup
retail_df.at[index, 'Current_Price'] = row['Base_Price'] * 1.05
elif row['Category'] == 'Gaming':
# Gaming products have holiday discount
retail_df.at[index, 'Current_Price'] = row['Base_Price'] * 0.9
else:
# Other categories maintain base price
retail_df.at[index, 'Current_Price'] = row['Base_Price']
# Apply stock-based adjustments
if row['Stock'] < 20:
# Low stock items get premium pricing
retail_df.at[index, 'Current_Price'] *= 1.10
elif row['Stock'] > 70:
# High stock items get discounted
retail_df.at[index, 'Current_Price'] *= 0.95
# Apply supplier-specific discounts
if row['Supplier'] == 'TechCorp':
# Premium supplier without discounts
pass
elif row['Supplier'] == 'AudioPro':
# Special partnership discount
retail_df.at[index, 'Current_Price'] *= 0.92
# Round prices to 2 decimal places
retail_df['Current_Price'] = retail_df['Current_Price'].round(2)
print("Retail products with updated pricing:")
print(retail_df)This example demonstrates how iterrows() can handle complex business logic, combining category rules, stock levels, and supplier relationships to determine the final product price.
Read Pandas str.replace Multiple Values in Python
When to Use iterrows() vs. Other Methods
While iterrows() is powerful, it’s not always the best choice. Here’s a quick guide on when to use it:
Use iterrows() when:
- You need to apply complex conditional logic
- You’re working with a relatively small DataFrame
- You need to access the index and row values simultaneously
- You’re updating multiple columns based on row values
Consider alternatives when:
- You’re working with very large DataFrames
- You’re performing simple operations that can be vectorized
- Performance is critical
- You’re not modifying the DataFrame in place
Remember that for simple operations, vectorized approaches like df['column'] = calculation are almost always faster and more readable than using iterrows().
I hope you found this article helpful for updating values in Pandas DataFrames using the iterrows() method.
While it’s not the fastest approach for every situation, iterrows() offers flexibility and intuitive row-by-row processing that makes it invaluable when working with complex data manipulation tasks.
You may like to read:
- Convert Python Dictionary to Pandas DataFrame
- Add Rows to a DataFrame Pandas in a Loop in Python
- Convert a Pandas DataFrame to a List in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.