Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Intermediate25 min
Updating Values hero Module 24 Lesson 12Updating Values hero Module 24 Lesson 12

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.

A flag column where only the two low-scoring rows were rewrittenA flag column where only the two low-scoring rows were rewritten

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.

python
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:

text
        hours  score
name                
Avery      42     90
Jordan     31     76
Casey      55     93
Riley      30     67
Morgan     47     84
Quinn      38     81

Only 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.

python
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:

text
     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         pass

where 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.

python
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:

text
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: int64

clip(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.

python
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:

text
     name          track
0   Avery    Core Python
1  Jordan          NumPy
2   Casey    Core Python
3   Riley  pandas Basics
4  Morgan          NumPy
5   Quinn  pandas Basics

Rename and round

rename changes labels rather than values, for columns or for the index. round tidies the numbers for a report.

python
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:

text
     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.08

Key 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

  • Next: Removing Columns and Rows

  • 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