Updating Row Values in a Python Dataframe
Dataframes are a fundamental data structure in the Pandas library that make it easy to work with tabular data. They are similar to a spreadsheet, with rows and columns that can be manipulated and analyzed in various ways using Python.
In this article, we will explore various methods of updating row values in a Python Dataframe.
Creating a Dataframe
Before we can update values in a Dataframe, we need to first create one. We can do this using the pd.DataFrame()
function, passing in a list or an array of data.
For example, let’s create a Dataframe of student grades.
import pandas as pd
data = {'Name': ['John', 'Lisa', 'Mike', 'Sarah'],
'Math': [85, 95, 72, 90],
'Science': [93, 88, 65, 80],
'English': [88, 91, 70, 85]}
df = pd.DataFrame(data)
print(df)
Output:
Name Math Science English
0 John 85 93 88
1 Lisa 95 88 91
2 Mike 72 65 70
3 Sarah 90 80 85
Using Python at()
method to update the value of a row
The at()
method in Pandas is used to access a single value in a Dataframe. We can use this method to update a single value in a row.
For example, let’s change Sarah’s Math grade to 95.
df.at[3, 'Math'] = 95
print(df)
Output:
Name Math Science English
0 John 85 93 88
1 Lisa 95 88 91
2 Mike 72 65 70
3 Sarah 95 80 85
Python loc()
function to change the value of a row/column
The loc()
function is more versatile than the at()
method when it comes to accessing and modifying values in a Dataframe. We can use this function to update multiple values in a row or column at once.
For example, let’s update all of Mike’s grades to 80.
df.loc[df['Name'] == 'Mike', ['Math', 'Science', 'English']] = 80
print(df)
Output:
Name Math Science English
0 John 85 93 88
1 Lisa 95 88 91
2 Mike 80 80 80
3 Sarah 95 80 85
Python replace()
method to update values in a dataframe
The replace()
method can be used to replace specific values in a Dataframe with new values. For example, let’s replace all instances of 80 with 85 in the Math column.
df['Math'] = df['Math'].replace(80, 85)
print(df)
Output:
Name Math Science English
0 John 85 93 88
1 Lisa 95 88 91
2 Mike 85 80 80
3 Sarah 95 80 85
Using iloc()
method to update the value of a row
Finally, we can use the iloc()
method to update a row by its integer index. For example, let’s update Lisa’s grades to 90 in one line of code.
df.iloc[1, 1:] = 90
print(df)
Output:
Name Math Science English
0 John 85 93 88
1 Lisa 90 90 90
2 Mike 85 80 80
3 Sarah 95 80 85
Conclusion
In this article, we have explored various techniques for updating row values in a Python Dataframe. We have demonstrated how to use the at()
, loc()
, and iloc()
methods to modify specific values or ranges of values in a DataFrame, as well as how to use the replace()
method to change all instances of a particular value.
By mastering these techniques, you will be able to efficiently manipulate and analyze your tabular data in Python with ease. To update row values in a Python Dataframe, there are various techniques available such as at()
, loc()
, iloc()
, and replace()
methods.
We can use the at()
method to modify a single value in a row while loc()
is used to modify multiple values in a row or column. The replace()
method is used to change all instances of a particular value.
Finally, we can use the iloc()
method to update a row using its integer index. With these techniques, we can efficiently manipulate and analyze our tabular data in Python with ease, emphasizing the importance of the topic.