0
0
PandasHow-ToBeginner · 3 min read

How to Rename Columns in pandas: Simple Guide with Examples

To rename columns in pandas, use the DataFrame.rename() method with a dictionary mapping old names to new names, or assign a new list of column names directly to DataFrame.columns. Both methods let you change column names easily and clearly.
📐

Syntax

The main ways to rename columns in pandas are:

  • DataFrame.rename(columns={'old_name': 'new_name'}): Rename specific columns by passing a dictionary.
  • DataFrame.columns = ['new_name1', 'new_name2', ...]: Replace all column names by assigning a new list.

The rename() method returns a new DataFrame by default, so use inplace=True to modify the original.

python
df.rename(columns={'old_name': 'new_name'}, inplace=True)
df.columns = ['new_name1', 'new_name2', 'new_name3']
💻

Example

This example shows how to rename specific columns using rename() and how to rename all columns by assigning a new list.

python
import pandas as pd

data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30], 'City': ['NY', 'LA']}
df = pd.DataFrame(data)

# Rename 'Name' to 'First Name' and 'City' to 'Location'
df_renamed = df.rename(columns={'Name': 'First Name', 'City': 'Location'})

# Rename all columns
new_columns = ['First Name', 'Age', 'Location']
df.columns = new_columns

print('DataFrame after rename():')
print(df_renamed)
print('\nDataFrame after assigning new columns list:')
print(df)
Output
DataFrame after rename(): First Name Age Location 0 Alice 25 NY 1 Bob 30 LA DataFrame after assigning new columns list: First Name Age Location 0 Alice 25 NY 1 Bob 30 LA
⚠️

Common Pitfalls

Common mistakes when renaming columns include:

  • Not using inplace=True with rename(), so changes don't affect the original DataFrame.
  • Assigning a new list to columns with a different length than the number of columns, causing errors.
  • Using incorrect column names in the dictionary, so no columns get renamed.
python
import pandas as pd

data = {'A': [1, 2], 'B': [3, 4]}
df = pd.DataFrame(data)

# Wrong: rename() without inplace, original df unchanged
new_df = df.rename(columns={'A': 'Alpha'})
print('Original df columns:', df.columns.tolist())
print('New df columns:', new_df.columns.tolist())

# Wrong: assigning wrong length list to columns
try:
    df.columns = ['Alpha']  # Only one name for two columns
except ValueError as e:
    print('Error:', e)

# Right: rename with inplace
df.rename(columns={'A': 'Alpha'}, inplace=True)
print('After inplace rename:', df.columns.tolist())
Output
Original df columns: ['A', 'B'] New df columns: ['Alpha', 'B'] Error: Length mismatch: Expected axis has 2 elements, new values have 1 elements After inplace rename: ['Alpha', 'B']
📊

Quick Reference

MethodDescriptionExample
rename()Rename specific columns using a dictionarydf.rename(columns={'old': 'new'}, inplace=True)
columns assignmentRename all columns by assigning a listdf.columns = ['new1', 'new2', 'new3']

Key Takeaways

Use df.rename(columns={old:new}, inplace=True) to rename specific columns safely.
Assign a full list to df.columns to rename all columns at once.
Always match the length of the new columns list to the number of columns.
Without inplace=True, rename() returns a new DataFrame and does not change the original.
Check column names carefully to avoid silent failures when renaming.