🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.rename()

AI & DATA SCIENCE // df-rename

df.rename() renames specific row labels or column names by mapping old names to new ones, leaving every other label untouched.

Syntax

df.rename(columns=None, index=None)

Deep Dive Course

rename()'s columns and index parameters each take a dict mapping existing labels to new ones — only the labels you explicitly mention get changed, and any label not present as a key in the dict is left exactly as it was. This targeted approach is usually more convenient than reassigning the whole df.columns list directly, especially when you only need to fix one or two column names rather than relabel every single one.

1Understanding df.rename()

rename()'s columns and index parameters each take a dict mapping existing labels to new ones — only the labels you explicitly mention get changed, and any label not present as a key in the dict is left exactly as it was. This targeted approach is usually more convenient than reassigning the whole df.columns list directly, especially when you only need to fix one or two column names rather than relabel every single one.

💡

By default, rename() returns a new DataFrame and leaves the original unchanged — pass inplace=True if you specifically want to modify the DataFrame directly instead of capturing rename()'s return value in a new variable.

editor.html
import pandas as pd

df = pd.DataFrame({"Name": ["Alice"], "Age": [30]})
df = df.rename(columns={"Name": "name", "Age": "age"})
print(df.columns.tolist())
localhost:3000

2Practical Example

Here is a real-world application of df.rename() showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"First Name": ["Alice"], "Last Name": ["Smith"]})
df = df.rename(columns=str.lower)
print(df.columns.tolist())
localhost:3000

3Best Practices

Follow these guidelines when working with df.rename():

1. Use rename(columns={'old': 'new'}) for targeted renaming of specific columns, rather than reassigning the entire df.columns list

2. Remember rename() returns a new DataFrame by default — either capture its return value or pass inplace=True to modify the original directly

3. Use a function instead of a dict for columns, like a lowercasing function, when you need to apply the same transformation to every column name at once

⚠️

Tip: By default, rename() returns a new DataFrame and leaves the original unchanged — pass inplace=True if you specifically want to modify the DataFrame directly instead of capturing rename()'s return value in a new variable.

editor.html
import pandas as pd

df = pd.DataFrame({"Name": ["Alice"], "Age": [30]})
df = df.rename(columns={"Name": "name", "Age": "age"})
print(df.columns.tolist())
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"Name": ["Alice"], "Age": [30]})
df = df.rename(columns={"Name": "name", "Age": "age"})
print(df.columns.tolist())
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"First Name": ["Alice"], "Last Name": ["Smith"]})
df = df.rename(columns=str.lower)
print(df.columns.tolist())

Best Practices

  • Use rename(columns={'old': 'new'}) for targeted renaming of specific columns, rather than reassigning the entire df.columns list
  • Remember rename() returns a new DataFrame by default — either capture its return value or pass inplace=True to modify the original directly
  • Use a function instead of a dict for columns, like a lowercasing function, when you need to apply the same transformation to every column name at once

Interview Question

Why does calling df.rename(columns={'old_name': 'new_name'}) followed by printing df still show the original column name, unless you capture the return value?

Hint: Think about rename()'s default behavior regarding the original DataFrame.

By default, rename() doesn't modify the DataFrame in place — it computes and returns a brand-new DataFrame with the renamed columns, leaving the original df object completely untouched. If you call rename() without either reassigning its result back to df or passing inplace=True, the renamed version is created and then immediately discarded, since nothing kept a reference to it, so the original df still has its old column names when you check it afterward.

Exercises

MediumPractice using df.rename() in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"Name": ["Alice"], "Age": [30]})
df = df.rename(columns={"Name": "name", "Age": "age"})
print(df.columns.tolist())

Frequently Asked Questions

Why does calling df.rename(columns={'old_name': 'new_name'}) followed by printing df still show the original column name, unless you capture the return value?

By default, rename() doesn't modify the DataFrame in place — it computes and returns a brand-new DataFrame with the renamed columns, leaving the original df object completely untouched. If you call rename() without either reassigning its result back to df or passing inplace=True, the renamed version is created and then immediately discarded, since nothing kept a reference to it, so the original df still has its old column names when you check it afterward.

Related Functions

df-columnsdf-dropseries-str-lower