Sometimes, we want to change one value based on another value in Python Pandas.
In this article, we’ll look at how to change one value based on another value in Python Pandas.
How to change one value based on another value in Python Pandas?
To change one value based on another value in Python Pandas, we can use the slicing feature.
For instance, we write
import pandas
df = pandas.read_csv("test.csv")
df.loc[df.ID == 103, 'FirstName'] = "Matt"
to read the test.csv into a data frame with read_csv
.
Then we use df.loc[df.ID == 103, 'FirstName']
to get the entry with ID
103 and the assign the FirstName
value of that entry to 'Matt'
.
Conclusion
To change one value based on another value in Python Pandas, we can use the slicing feature.