Rename specific columns in pandas
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Renaming a few columns in a pandas DataFrame is a small operation that shows up constantly in real data work. The cleanest tool is DataFrame.rename, because it lets you change only the columns you care about and leave everything else untouched.
Use rename(columns=...) for Targeted Changes
The standard pattern is to pass a dictionary that maps old names to new names:
Only the keys listed in the dictionary are renamed. last_name stays exactly as it was.
That is why rename is better than reassigning df.columns when you want a selective update. Replacing df.columns requires you to supply every column name in order, which is more fragile and harder to maintain.
Decide Whether to Reassign or Modify In Place
By default, rename returns a new DataFrame:
If you want the original variable to carry the updated names, assign the result back:
You may also see inplace=True, but explicit reassignment is often clearer because it makes the transformation visible in the code and plays better with method chaining.
Raise Errors on Typos When Needed
One subtle behavior surprises many people: by default, pandas ignores rename keys that do not exist. That can hide spelling mistakes.
If you want pandas to fail loudly when a column is missing, use errors="raise":
That is useful in ETL code where silent mismatches can cause later steps to break in harder-to-debug ways.
Rename Columns Conditionally
Sometimes you do not know the exact names in advance, but you still want a targeted transformation. In that case, pass a function or build the mapping dynamically.
For example, rename only columns that start with score_:
This keeps the logic explicit while still affecting only a subset of columns.
When df.columns = ... Is Appropriate
Direct assignment to df.columns is still valid when you truly want to rename every column and you already know the full final list:
That approach is not ideal for partial renames, because it couples the code to the full column order. If the source schema changes, the entire assignment can become wrong at once.
Method Chaining Example
Renaming often appears as part of a cleaning pipeline. Returning a new DataFrame makes that natural:
This style reads cleanly because each step is explicit and local.
Common Pitfalls
The most common mistake is forgetting that rename returns a new DataFrame by default. If you call it without assignment, the original object still has the old names.
Another pitfall is using df.columns = ... for a partial rename. That works only if you rewrite the full list, and it becomes brittle as soon as the schema changes.
It is also easy to miss typos because pandas ignores unknown rename keys unless you opt into errors="raise".
Finally, be careful with duplicate column names. Renaming is still possible, but later column selection can become confusing if duplicates remain in the result.
Summary
- Use
df.rename(columns={...})when you want to rename only specific columns. - Reassign the result back to
dfunless you deliberately want a separateDataFrame. - Prefer
renameoverdf.columns = ...for partial changes. - Use
errors="raise"when silent misses would be dangerous. - Build the rename mapping dynamically when column patterns matter more than exact names.
Related reading
- Renaming column names in Pandas
- Replace all elements of NumPy array that are greater than some value
- Replace nan values in tensorflow tensor
- Replacing blank values white space with NaN in pandas
- Repeating triangle pattern in Python
- Replace a character at a specific index in a string?
- Replacing column values in a pandas DataFrame
- Replacing Pandas or Numpy Nan with a None to use with MysqlDB
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.