How to Update Rows and Columns in Pandas DataFrames
Introduction
In this article we walk through practical techniques for modifying existing rows and columns in a pandas DataFrame. Building on earlier filtering concepts, we cover column renaming, bulk updates, single‑cell edits, and common pitfalls such as the SettingWithCopy warning.
Updating Column Names
- Rename all columns at once: assign a new list to
df.columns. - Transform column names (e.g., uppercase, replace spaces): use a list comprehension or string methods:
python df.columns = [col.upper() for col in df.columns] df.columns = df.columns.str.replace(' ', '_') - Rename specific columns: use
df.rename(columns={'old_name':'new_name'}, inplace=True).
Editing Row Data
Single‑row, multiple‑column update
df.loc[2, ['last_name','email']] = ['Smith','[email protected]']
Single‑cell update
- With
.loc:df.loc[2, 'last_name'] = 'Smith' - With
.at(faster for scalar access):df.at[2, 'last_name'] = 'Smith'
Common mistake – SettingWithCopy warning
Assigning to a slice obtained via plain indexing (e.g., df[mask]['col'] = value) creates a temporary view, so the original DataFrame is unchanged. The safe approach is to use .loc or .at with the mask:
df.loc[mask, 'last_name'] = 'Smith'
Bulk Row Updates
- Lower‑casing an entire column:
python df['email'] = df['email'].str.lower() - Applying a custom function with
apply(Series) orapplymap(DataFrame):python df['email'] = df['email'].apply(lambda x: x.upper()) df = df.applymap(str.lower) # applies to every element - Using
mapfor substitution (Series only):python df['first_name'] = df['first_name'].map({'Quarry':'Chris','Jane':'Mary'}) - Using
replacewhen you want untouched values to stay unchanged:python df['first_name'] = df['first_name'].replace({'Quarry':'Chris','Jane':'Mary'})
When to Use apply, applymap, map, and replace
| Method | Works on | Typical Use |
|---|---|---|
apply | Series or DataFrame | Apply a function to each column (default) or each row (axis=1). |
applymap | DataFrame | Apply a function element‑wise across the whole frame. |
map | Series | Substitute values based on a dict, Series, or function. |
replace | Series or DataFrame | Replace specific values; leaves non‑matched entries unchanged. |
Real‑World Example with Stack Overflow Survey Data
- Rename a column – change
converted_comptosalary_usdusingdf.renamewithinplace=True. - Map Yes/No to booleans –
python df['hobbyist'] = df['hobbyist'].map({'Yes':True,'No':False}) - Lower‑case all string entries –
df = df.applymap(lambda x: x.lower() if isinstance(x,str) else x).
Conclusion
Updating DataFrames is straightforward once you master the indexers (.loc, .at) and the transformation helpers (rename, apply, applymap, map, replace). Always prefer explicit indexers to avoid the SettingWithCopy warning, and choose the appropriate method based on whether you are working with whole columns, rows, or individual elements.
Key Takeaway
Use .loc/.at for safe, precise cell updates, and leverage apply, applymap, map, and replace to perform bulk transformations efficiently.
Mastering pandas' indexing and transformation functions lets you modify DataFrames reliably, avoid common pitfalls, and keep your data cleaning pipeline clean and performant.
Frequently Asked Questions
Who is Corey Schafer on YouTube?
Corey Schafer is a YouTube channel that publishes videos on a range of topics. Browse more summaries from this channel below.
Does this page include the full transcript of the video?
Yes, the full transcript for this video is available on this page. Click 'Show transcript' in the sidebar to read it.
When to Use `apply`, `applymap`, `map`, and `replace`
| Method | Works on | Typical Use | |--------|----------|-------------| | `apply` | Series or DataFrame | Apply a function to each **column** (default) or each **row** (`axis=1`). | | `applymap` | DataFrame | Apply a function element‑wise across the whole frame. | | `map` | Series | Substitute values based on a dict, Series, or function. | | `replace` | Series or DataFrame | Replace specific values; leaves non‑matched entries unchanged. |
Helpful resources related to this video
If you want to practice or explore the concepts discussed in the video, these commonly used tools may help.
Links may be affiliate links. We only include resources that are genuinely relevant to the topic.