Programmer's Picnic · Python / Pandas · Lesson 2

Change and Update DataFrames

Correct a value, modify only matching rows, calculate new columns and add or remove records without fragile chained assignment.

0 to infinity · Learn by making real DataFrames · No file required

Start from this table

Each exercise below rebuilds the same sample DataFrame, so changes in one example do not accidentally affect the next.

RowNameCityMarksFeePaid
0AshaVaranasi88True
1RaviRanchi42False
2MeeraVaranasi76True
3KabirPatna91False
Python · pandas
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]
})
Modern pandas rule: Edit a DataFrame using a single .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.
Step 1

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.

Python · pandas
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.

Step 2

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.

Python · pandas
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.

Step 3

Update many matching rows together

Use a mask to change several rows in a single operation, keeping the others unchanged.

Python · pandas
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.

Step 4

Add calculated or descriptive columns

Assign a new column directly; its values are calculated row by row. Then update some statuses.

Python · pandas
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.

Step 5

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.

Python · pandas
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.

Step 6

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.

Python · pandas
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.

Step 7

Remove a row or column

Create a filtered DataFrame when removing rows, or use drop() for named columns. Assign the result to keep it.

Python · pandas
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).

Step 8

Update with two conditions

Combine the search skills from Lesson 1 and the editing skills from Lesson 2.

Python · pandas
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
Python · pandas
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.

Python · pandas
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.

← Previous lessonCourse indexNext lesson: DataFrame cleaning →