How can I update values in Pandas using `iterrows`?

How can I update values in Pandas using `iterrows`?

The `iterrows` function in Pandas allows for updating values in a DataFrame by iterating through each row of the data. This method can be used to modify specific values or perform calculations on multiple columns at once. By accessing the index and values of each row, the `iterrows` function allows for efficient and customizable updates to be made to a Pandas DataFrame.

Pandas: Update Values in iterrows


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 .

The following tutorials explain how to perform other common tasks in pandas:

Cite this article

stats writer (2024). How can I update values in Pandas using `iterrows`?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-update-values-in-pandas-using-iterrows/

stats writer. "How can I update values in Pandas using `iterrows`?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-update-values-in-pandas-using-iterrows/.

stats writer. "How can I update values in Pandas using `iterrows`?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-update-values-in-pandas-using-iterrows/.

stats writer (2024) 'How can I update values in Pandas using `iterrows`?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-update-values-in-pandas-using-iterrows/.

[1] stats writer, "How can I update values in Pandas using `iterrows`?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I update values in Pandas using `iterrows`?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top