You can use the following basic syntax to update values in a pandas DataFrame while using iterrows:
for i, row in df.iterrows():
points_add = 10
if row['points'] > 15:
points_add = 50
df.at[i,'points'] = points_add
This particular example iterates over each row in a DataFrame and updates the value in the points column to be 50 if the value is currently greater than 15.
If the current value is less than or equal to 15, the value is updated to be 10.
The following example shows how to use this syntax in practice.
Example: Update Values in Pandas DataFrame in iterrows
Suppose we have the following pandas DataFrame that shows the number of points scored by various basketball players:
import pandas as pd #create DataFrame df = pd.DataFrame({'player': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'], 'points': [10, 12, 14, 15, 15, 15, 16, 17, 20]}) #view DataFrame print(df) player points 0 A 10 1 B 12 2 C 14 3 D 15 4 E 15 5 F 15 6 G 16 7 H 17 8 I 20
Suppose we would like to update the values in the points column using the following logic:
- If points is less than or equal to 15, update the value to be 10.
- If points is greater than 15, update the value to be 50.
We can use the iterrows function to iterate over each row in the DataFrame and make these updates:
#iterate over each row in DataFrame and update values in points column
for i, row in df.iterrows():
points_add = 10
if row['points'] > 15:
points_add = 50
df.at[i,'points'] = points_add
#view updated DataFrame
print(df)
player points
0 A 10
1 B 10
2 C 10
3 D 10
4 E 10
5 F 10
6 G 50
7 H 50
8 I 50
We can see that the values in the points column have been updated accordingly.
Note: You can find the complete documentation for the pandas iterrows() function here.
Additional Resources
The following tutorials explain how to perform other common tasks in pandas:
Pandas: How to Iterate Over Columns
Pandas: How to Select Rows Between Two Values
Pandas: Update Column Values Based on Another DataFrame