Hello, readers! In this article, we will be focusing on different ways to update the value of a row in a Python Dataframe in detail.
So, let us get started!
First, where do rows and columns reside?
In Python programming language, we come across this module called Pandas which offers us a data structure called a data frame.
A data frame stores data in it in the form of rows and columns. Thus, it can be considered as a matrix and is useful while analyzing the data.
Let us created a dataframe right away!
import pandas as pd
info= {"Num":[12,14,13,12,14,13,15], "NAME":['John','Camili','Rheana','Joseph','Amanti','Alexa','Siri']}
data = pd.DataFrame(info)
print("Original Data frame:\n")
print(data)
Here, we have created a data frame using pandas.DataFrame()
function
Output:
Original Data frame:
Num NAME
0 12 John
1 14 Camili
2 13 Rheana
3 12 Joseph
4 14 Amanti
5 13 Alexa
6 15 Siri
We will be using the above created data frame in the entire article for reference with respect to examples.
1. Using Python at() method to update the value of a row
Python at() method enables us to update the value of one row at a time with respect to a column.
Syntax:
dataframe.at[index,'column-name']='new value'
Example:
In this example, we have provided the at() function with index 6 of the data frame and column ‘NAME’. Thus, the value of the column ‘NAME’ at row index 6 gets updated.
data.at[6,'NAME']='Safa'
Output:
Num NAME
0 12 John
1 14 Camili
2 13 Rheana
3 12 Joseph
4 14 Amanti
5 13 Alexa
6 15 Safa
2. Python loc() function to change the value of a row/column
Python loc() method can also be used to update the value of a row with respect to columns by providing the labels of the columns and the index of the rows.
Syntax:
dataframe.loc[row index,['column-names']] = value
Example:
data.loc[0:2,['Num','NAME']] = [100,'Python']
Here, we have updated the value of the rows from index 0 to 2 with respect to columns ‘Num’ and ‘NAME’, respectively.
Output:
Num NAME
0 100 Python
1 100 Python
2 100 Python
3 12 Joseph
4 14 Amanti
5 13 Alexa
6 15 Siri
3. Python replace() method to update values in a dataframe
Using Python replace() method, we can update or change the value of any string within a data frame. We need not provide the index or label values to it.
Syntax:
dataframe.replace("old string", "new string")
Example:
data.replace("Siri",
"Code",
inplace=True)
As seen above, we have replaced the word “Siri” with “Code” within the dataframe.
Output:
Num NAME
0 12 John
1 14 Camili
2 13 Rheana
3 12 Joseph
4 14 Amanti
5 13 Alexa
6 15 Code
4. Using iloc() method to update the value of a row
With the Python iloc() method, it is possible to change or update the value of a row/column by providing the index values of the same.
Syntax:
dataframe.iloc[index] = value
Example:
data.iloc[[0,1,3,6],[0]] = 100
In this example, we have updated the value of the rows 0, 1, 3 and 6 with respect to the first column i.e. ‘Num’ to 100.
We can even provide the function with slicing of rows to change the values of multiple rows consequently using iloc() function.
Output:
Num NAME
0 100 John
1 100 Camili
2 13 Rheana
3 100 Joseph
4 14 Amanti
5 13 Alexa
6 100 Siri
Conclusion
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.
For more such posts related to Python, stay tuned and till then, Happy Learning!! 🙂