Pandas Update Row Value In Python DataFrame

It is our goal in this article to provide you with an in-depth understanding of the various methods to update row value in a Python Dataframe.

Where Are Rows And Columns In Pandas DataFrame?

A data frame is a data structure that we can use in the Python programming language. We come across this module called Pandas in the Python programming language.

A data frame is a data structure that stores information in the form of rows and columns. Because of this, it can be thought of as a matrix, which is useful when analyzing the information. Let’s get started on creating a dataframe right away.

#importing Pandas Library
import pandas as pd

#Below is the list of Students with Roll Numeber
studentList = {"Roll No.":[1,2,3,4,5], 
"NAME":['Adam', 'Bella', 'Chad', 'Doug', 'Emily']}
 
data = pd.DataFrame(studentList)
print("List Of Students in DataFrame:\n")
print(data)

Output:

   Roll No.   NAME
0         1   Adam
1         2  Bella
2         3   Chad
3         4   Doug
4         5  Emily

In this example, we have used pandas to create a data frame. The DataFrame() function is used to display data frames. We will be referencing the data frame that was created above throughout the entire article in order to provide examples of what we mean.

1. Using at() Function To Update Row Value In Python DataFrame

The at() method in Python allows us to update the value of one row at a time with respect to a column by using a nested loop.

Syntax:

dataframe.at[index,'column-name']='new value'

where, 
         index = row number you want to update.
   column-name = title of the column you want to update.
     new value = the new value to want the old value to be replaced with.

As shown in this example, we have provided the at() function with index 3 of the data frame and the value ‘NAME’ for the first column. It is thus necessary to make an update to the value of the column ‘NAME’ at row index 3.

#importing Pandas Library
import pandas as pd

#Below is the list of Students with Roll Numeber
studentList = {"Roll No.":[1,2,3,4,5], 
"NAME":['Adam', 'Bella', 'Chad', 'Doug', 'Emily']}
 
studentData = pd.DataFrame(studentList)

#Updating the Row Value with At Function
studentData.at[2, 'NAME'] = 'Carl'

print("List Of Students in DataFrame:\n")
print(studentData)

Output:

   Roll No.   NAME
0         1   Adam
1         2  Bella
2         3   Carl
3         4   Doug
4         5  Emily

2. Using loc() Function To Change The Value Of a Row or Column

The loc() method in Python can also be used to update the value of a row with respect to columns, provided that the labels of the columns and the index of the rows are passed to the function.

Syntax:

dataframe.loc[row index,['column-names']] = new value

Let us see the code example for the usage of the function loc() in the Python dataframe.

#importing Pandas Library
import pandas as pd

#Below is the list of Students with Roll Numeber
studentList = {"Roll No.":[1,2,3,4,5], 
"NAME":['Adam', 'Bella', 'Chad', 'Doug', 'Emily']}
 
studentData = pd.DataFrame(studentList)

#Updating the Row Value with At Function
studentData.loc[0,['Roll No.','NAME']] = [1, 'Ace']

print("List Of Students in DataFrame:\n")
print(studentData)
   Roll No.   NAME
0         1    Ace
1         2  Bella
2         3   Chad
3         4   Doug
4         5  Emily

As you can see in this example, we have changed the value of the row number 0 in relation to the columns “Roll No.” and “NAME.”

3. Using iloc() Function To Update The Value Of A Row

A row or column can be updated or changed using the Python iloc() method by providing the index values for the same.

Syntax:

dataframe.iloc[index] = new value

where, 
         index = row number you want to update.
     new value = the new value to want the old value to be replaced with.
#importing Pandas Library
import pandas as pd

#Below is the list of Students with Roll Numeber
studentList = {"NAME":['Adam', 'Bella', 'Chad', 'Doug', 'Emily'], "Marks" : [30,40,60,80,90]}
 
studentData = pd.DataFrame(studentList)

#Updating the Row Value with At Function
studentData.iloc[[0,2,4],[1]] = 75

print("List Of Students in DataFrame:\n")
print(studentData)

Output:

    NAME  Marks
0   Adam     75
1  Bella     40
2   Chad     75
3   Doug     80
4  Emily     75

Rows 0, 2, and 4 have been updated with respect to the second column ‘Marks’ to 75 in this example.
Using the iloc() function, we can even change the values of multiple rows by slicing the rows.

4. Using replace() Function To Update Row Or Column in Pandas DataFrame

We can update or change the value of any string contained within a data frame by using the Python replace() method. It is not necessary for us to provide the index or label values to it.

Syntax:

dataframe.replace("valueToReplace", "newValue")
#importing Pandas Library
import pandas as pd

#Below is the list of Students with Roll Numeber
studentList = {"NAME":['Adam', 'Bella', 'Chad', 'Doug', 'Emily'], "Marks" : [30,40,60,80,90]}
 
studentData = pd.DataFrame(studentList)

#Updating the Row Value with At Function
studentData.replace("Adam", "Ace", inplace=True)

print("List Of Students in DataFrame:\n")
print(studentData)

Output:

    NAME  Marks
0    Ace     30
1  Bella     40
2   Chad     60
3   Doug     80
4  Emily     90
Pandas Update Row Value In Python DataFrame

As you can see in the example above, the word “Adam” has been replaced with the word “Ace” within the dataframe.

If you have any problems, please let me know in the comments section and I will gladly assist you as soon as possible.

Further Read:

  1. [Fixed] Python TypeError: unhashable type: ‘list’
  2. Python How To Add A New Line To A String
  3. Python PermissionError: [Errno 13] Permission denied Fix
  4. Python ValueError: could not convert string to float [Fix]
  5. Python pip: command not found Quick Fix

Leave a Comment