Lesson 12
Updating Values
Change what is already in a DataFrame: one cell, a whole column, or only the rows that match a condition, with loc, where, mask, and replace.
Updating is selecting, then assigning. Pick the cells with .loc and put a value on the right of the =. Because .loc points at the real table, the change sticks.
One cell and one column
.loc[row, column] names a single cell. Assigning to a column name that already exists replaces the whole column, and an expression on the right lets you rescale it.
import pandas as pd
learners = pd.DataFrame({
"name": ["Avery", "Jordan", "Casey", "Riley", "Morgan", "Quinn"],
"track": ["Python", "NumPy", "Python", "pandas", "NumPy", "pandas"],
"city": ["Northgate", "Eastport", "Northgate", "Westfield", "Eastport", "Westfield"],
"hours": [42, 31, 55, 24, 47, 38],
"score": [88, 74, 91, 65, 82, 79],
})
tagged = learners.set_index("name")
tagged.loc["Riley", "hours"] = 30
tagged["score"] = tagged["score"] + 2
print(tagged[["hours", "score"]])Output:
hours score
name
Avery 42 90
Jordan 31 76
Casey 55 93
Riley 30 67
Morgan 47 84
Quinn 38 81Only the matching rows
Put a mask where the row label would go. Every row the mask marks True gets the new value, and the rest are left alone. This is the pandas way to say if-then over a whole table.
import pandas as pd
learners = pd.DataFrame({
"name": ["Avery", "Jordan", "Casey", "Riley", "Morgan", "Quinn"],
"track": ["Python", "NumPy", "Python", "pandas", "NumPy", "pandas"],
"city": ["Northgate", "Eastport", "Northgate", "Westfield", "Eastport", "Westfield"],
"hours": [42, 31, 55, 24, 47, 38],
"score": [88, 74, 91, 65, 82, 79],
})
learners["grade"] = "pass"
learners.loc[learners["score"] >= 85, "grade"] = "distinction"
learners.loc[learners["score"] < 70, "grade"] = "retry"
print(learners[["name", "score", "grade"]])Output:
name score grade
0 Avery 88 distinction
1 Jordan 74 pass
2 Casey 91 distinction
3 Riley 65 retry
4 Morgan 82 pass
5 Quinn 79 passwhere and mask
where keeps the values whose condition is True and replaces the rest. mask is the mirror image: it replaces the True ones. Both return a new column, so nothing changes until you assign it.
import pandas as pd
learners = pd.DataFrame({
"name": ["Avery", "Jordan", "Casey", "Riley", "Morgan", "Quinn"],
"track": ["Python", "NumPy", "Python", "pandas", "NumPy", "pandas"],
"city": ["Northgate", "Eastport", "Northgate", "Westfield", "Eastport", "Westfield"],
"hours": [42, 31, 55, 24, 47, 38],
"score": [88, 74, 91, 65, 82, 79],
})
print(learners["hours"].where(learners["hours"] >= 30, 30))
print(learners["hours"].mask(learners["hours"] > 50, 50))Output:
0 42
1 31
2 55
3 30
4 47
5 38
Name: hours, dtype: int64
0 42
1 31
2 50
3 24
4 47
5 38
Name: hours, dtype: int64clip(30, 50) does both ends at once when you just want a floor and a ceiling.
replace swaps known values
replace takes a dict of old value to new value, which is the tidy way to fix spellings or shorten labels across a column.
import pandas as pd
learners = pd.DataFrame({
"name": ["Avery", "Jordan", "Casey", "Riley", "Morgan", "Quinn"],
"track": ["Python", "NumPy", "Python", "pandas", "NumPy", "pandas"],
"city": ["Northgate", "Eastport", "Northgate", "Westfield", "Eastport", "Westfield"],
"hours": [42, 31, 55, 24, 47, 38],
"score": [88, 74, 91, 65, 82, 79],
})
learners["track"] = learners["track"].replace({
"Python": "Core Python",
"pandas": "pandas Basics",
})
print(learners[["name", "track"]])Output:
name track
0 Avery Core Python
1 Jordan NumPy
2 Casey Core Python
3 Riley pandas Basics
4 Morgan NumPy
5 Quinn pandas BasicsRename and round
rename changes labels rather than values, for columns or for the index. round tidies the numbers for a report.
import pandas as pd
learners = pd.DataFrame({
"name": ["Avery", "Jordan", "Casey", "Riley", "Morgan", "Quinn"],
"track": ["Python", "NumPy", "Python", "pandas", "NumPy", "pandas"],
"city": ["Northgate", "Eastport", "Northgate", "Westfield", "Eastport", "Westfield"],
"hours": [42, 31, 55, 24, 47, 38],
"score": [88, 74, 91, 65, 82, 79],
})
learners["per_hour"] = learners["score"] / learners["hours"]
report = learners.rename(columns={"per_hour": "score_per_hour"}).round(2)
print(report[["name", "score_per_hour"]])Output:
name score_per_hour
0 Avery 2.10
1 Jordan 2.39
2 Casey 1.65
3 Riley 2.71
4 Morgan 1.74
5 Quinn 2.08Key takeaways
-
.loc[mask, column] = value writes only to the matching rows
-
Assigning to an existing column name replaces the whole column
-
where keeps the True values and mask replaces them
-
replace swaps known values and rename changes labels
-
Previous: Adding Columns and Rows
Loading playground…
Check your understanding
Updating Values Quiz
Which form writes only to the rows a condition marks?
1 / 10