Change and Update DataFrames
Correct a value, modify only matching rows, calculate new columns and add or remove records without fragile chained assignment.
Start from this table
Each exercise below rebuilds the same sample DataFrame, so changes in one example do not accidentally affect the next.
| Row | Name | City | Marks | FeePaid |
|---|---|---|---|---|
| 0 | Asha | Varanasi | 88 | True |
| 1 | Ravi | Ranchi | 42 | False |
| 2 | Meera | Varanasi | 76 | True |
| 3 | Kabir | Patna | 91 | False |
import pandas as pd
students = pd.DataFrame({
"Name": ["Asha", "Ravi", "Meera", "Kabir"],
"City": ["Varanasi", "Ranchi", "Varanasi", "Patna"],
"Marks": [88, 42, 76, 91],
"FeePaid": [True, False, True, False]
}).loc[row_selection, column_selection] operation. Avoid expressions such as students["Marks"][mask] = 50 — chained assignment does not update the original under Copy-on-Write.Correct a specific student's marks
Select a row by condition and the column to edit in one step. Print the updated row to verify.
import pandas as pd
students = pd.DataFrame({
"Name": ["Asha", "Ravi", "Meera", "Kabir"],
"City": ["Varanasi", "Ranchi", "Varanasi", "Patna"],
"Marks": [88, 42, 76, 91],
"FeePaid": [True, False, True, False]
})
students.loc[students["Name"] == "Ravi", "Marks"] = 52
print(students.loc[students["Name"] == "Ravi"])Remember: This updates the original DataFrame directly and intentionally.
Edit one cell with .at or .iat
Use .at when you already know the row and column label. Use .iat when you know their integer positions.
import pandas as pd
students = pd.DataFrame({
"Name": ["Asha", "Ravi", "Meera", "Kabir"],
"City": ["Varanasi", "Ranchi", "Varanasi", "Patna"],
"Marks": [88, 42, 76, 91],
"FeePaid": [True, False, True, False]
})
students.at[0, "City"] = "Prayagraj"
students.iat[1, 2] = 49
print(students)Remember: Use the right accessor for your indexing scheme; label 0 and position 0 only coincide with the default RangeIndex.
Update many matching rows together
Use a mask to change several rows in a single operation, keeping the others unchanged.
import pandas as pd
students = pd.DataFrame({
"Name": ["Asha", "Ravi", "Meera", "Kabir"],
"City": ["Varanasi", "Ranchi", "Varanasi", "Patna"],
"Marks": [88, 42, 76, 91],
"FeePaid": [True, False, True, False]
})
students.loc[students["Marks"] < 50, "Marks"] = 50
students.loc[students["City"] == "Varanasi", "FeePaid"] = True
print(students)Remember: A conditional assignment is safer than manually searching and editing each matching row.
Add calculated or descriptive columns
Assign a new column directly; its values are calculated row by row. Then update some statuses.
import pandas as pd
students = pd.DataFrame({
"Name": ["Asha", "Ravi", "Meera", "Kabir"],
"City": ["Varanasi", "Ranchi", "Varanasi", "Patna"],
"Marks": [88, 42, 76, 91],
"FeePaid": [True, False, True, False]
})
students["Bonus"] = 5
students["FinalMarks"] = (students["Marks"] + students["Bonus"]).clip(upper=100)
students["Status"] = "On track"
students.loc[students["Marks"] < 50, "Status"] = "Needs practice"
print(students[["Name", "FinalMarks", "Status"]])Remember: The new columns align with the existing rows.
Replace text values and rename a column
You can produce a modified Series, then assign it back. Rename a column and retain the returned DataFrame.
import pandas as pd
students = pd.DataFrame({
"Name": ["Asha", "Ravi", "Meera", "Kabir"],
"City": ["Varanasi", "Ranchi", "Varanasi", "Patna"],
"Marks": [88, 42, 76, 91],
"FeePaid": [True, False, True, False]
})
students["City"] = students["City"].replace({"Ranchi": "Ranchi City"})
students = students.rename(columns={"Marks": "Score"})
print(students)Remember: Assign the result of operations such as rename() when you want to keep the change.
Add another student
For a simple default row index, add a new row using a matching list of values. For large collections, gather records before constructing a DataFrame.
import pandas as pd
students = pd.DataFrame({
"Name": ["Asha", "Ravi", "Meera", "Kabir"],
"City": ["Varanasi", "Ranchi", "Varanasi", "Patna"],
"Marks": [88, 42, 76, 91],
"FeePaid": [True, False, True, False]
})
students.loc[len(students)] = ["Nina", "Varanasi", 83, True]
print(students.tail(2))Remember: Values must match the DataFrame's column order. This simple pattern assumes the default consecutive row index.
Remove a row or column
Create a filtered DataFrame when removing rows, or use drop() for named columns. Assign the result to keep it.
import pandas as pd
students = pd.DataFrame({
"Name": ["Asha", "Ravi", "Meera", "Kabir"],
"City": ["Varanasi", "Ranchi", "Varanasi", "Patna"],
"Marks": [88, 42, 76, 91],
"FeePaid": [True, False, True, False]
})
students = students.loc[students["Name"] != "Ravi"].copy()
students = students.drop(columns=["FeePaid"])
print(students)Remember: A new filtered DataFrame has the original row labels until you explicitly reset_index(drop=True).
Update with two conditions
Combine the search skills from Lesson 1 and the editing skills from Lesson 2.
import pandas as pd
students = pd.DataFrame({
"Name": ["Asha", "Ravi", "Meera", "Kabir"],
"City": ["Varanasi", "Ranchi", "Varanasi", "Patna"],
"Marks": [88, 42, 76, 91],
"FeePaid": [True, False, True, False]
})
mask = (students["City"] == "Varanasi") & (students["Marks"] >= 75)
students.loc[mask, "Marks"] = (students.loc[mask, "Marks"] + 3).clip(upper=100)
print(students[["Name", "Marks"]])Remember: The assignment targets the DataFrame once. This pattern works with pandas Copy-on-Write.
Practice C · Fix and reward marks
Correct Ravi's marks to 55, give all students scoring at least 80 a bonus of 5 (maximum 100), and display the final results.
Reveal a possible solution
import pandas as pd
students = pd.DataFrame({
"Name": ["Asha", "Ravi", "Meera", "Kabir"],
"City": ["Varanasi", "Ranchi", "Varanasi", "Patna"],
"Marks": [88, 42, 76, 91],
"FeePaid": [True, False, True, False]
})
students.loc[students["Name"] == "Ravi", "Marks"] = 55
mask = students["Marks"] >= 80
students.loc[mask, "Marks"] = (students.loc[mask, "Marks"] + 5).clip(upper=100)
print(students[["Name", "Marks"]])Capstone — one script from creation to correction
Here is the entire beginner workflow in one program: create → search → change → verify.
import pandas as pd
students = pd.DataFrame({
"Name": ["Asha", "Ravi", "Meera", "Kabir"],
"City": ["Varanasi", "Ranchi", "Varanasi", "Patna"],
"Marks": [88, 42, 76, 91],
"FeePaid": [True, False, True, False]
})
# Find students who need help.
print("Before correction:")
print(students.loc[students["Marks"] < 50, ["Name", "Marks"]])
# Correct a score and give a small reward to high scorers.
students.loc[students["Name"] == "Ravi", "Marks"] = 52
students["Eligible"] = students["Marks"] >= 75
print("After correction:")
print(students[["Name", "Marks", "Eligible"]])Official guidance: Copy-on-Write and chained assignment. Continue with Lesson 3: cleaning missing values, duplicates and invalid data.