Pandas
DataFrame
Python
Data Manipulation
Row Modification

Modifying a subset of rows in a pandas dataframe

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In pandas, the safest way to modify a subset of rows is usually to build a boolean mask and assign through .loc. That approach is explicit, vectorized, and avoids the most common trap in pandas updates: chained assignment that looks like it worked but changed a temporary object instead.

Use a boolean mask with .loc

Suppose you want to raise salaries for employees in the "sales" department:

python
1import pandas as pd
2
3df = pd.DataFrame({
4    "name": ["Ana", "Ben", "Cara", "Dan"],
5    "department": ["sales", "engineering", "sales", "hr"],
6    "salary": [50000, 70000, 52000, 48000]
7})
8
9mask = df["department"] == "sales"
10df.loc[mask, "salary"] = df.loc[mask, "salary"] * 1.10
11
12print(df)

The important part is that both the row filter and the target column are expressed through .loc. That tells pandas you are updating the original frame.

Update multiple columns at once

.loc also works cleanly when several columns need to change together:

python
1mask = df["department"] == "sales"
2df.loc[mask, ["salary", "department"]] = [
3    [55000, "sales-updated"],
4    [57200, "sales-updated"]
5]

For computed assignments, you can keep the operation vectorized:

python
df.loc[mask, ["salary"]] = df.loc[mask, ["salary"]] + 1000

Vectorized updates are usually faster and clearer than looping row by row.

Use conditions for selective updates

Conditional modification often reads best when the mask is named:

python
high_salary = df["salary"] > 60000
df.loc[high_salary, "bonus_eligible"] = True
df.loc[~high_salary, "bonus_eligible"] = False

You can also use where or mask, but .loc is usually the clearest starting point when the goal is "change these rows in this column."

Avoid chained assignment

This pattern is risky:

python
df[df["department"] == "sales"]["salary"] = 0

It may trigger a SettingWithCopyWarning, and the warning exists for a good reason. The intermediate expression may be a temporary object rather than the original DataFrame, so your update may not land where you think it does.

The safe version is:

python
df.loc[df["department"] == "sales", "salary"] = 0

That is one of the most important pandas habits to develop early.

Assignment can use aligned Series values

Pandas aligns by index, so you can assign values from another Series as long as the indexes line up:

python
adjustments = pd.Series({0: 1000, 2: 1500})
df.loc[adjustments.index, "salary"] = df.loc[adjustments.index, "salary"] + adjustments

This is useful when the rows to change have already been identified elsewhere in the pipeline.

Keep data types in mind

Partial updates can silently change column dtypes if the assigned values are incompatible. For example, inserting strings into an integer column may coerce the column to an object dtype. That may be acceptable, but it should be deliberate.

When a modification produces strange downstream behavior, checking df.dtypes is a good first step.

Inspect the target subset before writing

For non-trivial masks, it is often worth printing df.loc[mask] first. Confirming the selected rows before assignment is one of the fastest ways to prevent accidental bulk edits.

Common Pitfalls

  • Using chained assignment instead of .loc and updating a temporary slice.
  • Looping row by row when a vectorized assignment would be simpler and faster.
  • Forgetting that boolean masks must align with the DataFrame index.
  • Accidentally changing column dtypes through mixed-type assignments.
  • Updating the wrong subset because the mask condition was not inspected first.

Summary

  • Use .loc with a boolean mask to modify a subset of rows safely.
  • Vectorized assignments are usually clearer and faster than loops.
  • Chained assignment is the main pandas trap to avoid.
  • Named masks make conditional updates easier to read and debug.
  • Watch column dtypes after partial updates, especially with mixed-value assignments.

Course illustration
Course illustration

All Rights Reserved.