How to rename a column in Python

Learn how to rename a column in Python. This guide covers various methods, tips, real-world applications, and common error debugging.

How to rename a column in Python
Published on: 
Tue
Feb 24, 2026
Updated on: 
Mon
Apr 6, 2026
The Replit Team

The ability to rename columns in Python is fundamental for data cleaning. This simple step improves dataset clarity and prepares your data for effective analysis and modeling.

Here, you'll learn several techniques, including the rename() method. We'll also cover real-world applications and debugging tips to help you handle your data with confidence and precision.

Basic column renaming with df.columns

import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
print("Original columns:", df.columns.tolist())
df.columns = ['X', 'Y']
print("Renamed columns:", df.columns.tolist())--OUTPUT--Original columns: ['A', 'B']
Renamed columns: ['X', 'Y']

Directly assigning a new list to the df.columns attribute is the most straightforward way to rename every column in your DataFrame. This method is quick and effective, especially during initial data setup when you want to replace default or unclear headers.

The key is that you're replacing the entire column index. This means:

  • The new list must have the same number of elements as there are columns.
  • You can't use this to target and rename just one or two specific columns.

It's an all-or-nothing approach, perfect for a complete name overhaul.

Standard column renaming techniques

When you need more precision than replacing all columns at once, the rename() method offers a flexible and powerful alternative for targeted adjustments.

Using the rename() method with a dictionary

import pandas as pd
df = pd.DataFrame({'old_name': [1, 2, 3], 'B': [4, 5, 6]})
renamed_df = df.rename(columns={'old_name': 'new_name'})
print(renamed_df.columns.tolist())--OUTPUT--['new_name', 'B']

For selective renaming, the rename() method is your go-to tool. You pass a dictionary to its columns parameter, mapping old column names to new ones. This approach lets you change just the columns you need without affecting the rest of the DataFrame.

  • The dictionary provides a clear {'old_name': 'new_name'} mapping for each column you want to change.
  • By default, rename() returns a new DataFrame, leaving your original data untouched unless you specify inplace=True.

Using the rename() method with a function

import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
renamed_df = df.rename(columns=lambda x: x.lower() + '_col')
print(renamed_df.columns.tolist())--OUTPUT--['a_col', 'b_col']

For systematic renaming, you can pass a function to the columns parameter. This approach is powerful when you need to apply the same transformation to every column name, such as standardizing case or adding a consistent prefix or suffix.

  • The lambda function processes each column name individually.
  • In this case, it takes each name, converts it to lowercase with lower(), and appends _col, creating a uniform naming scheme across the DataFrame.

Renaming multiple columns at once

import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
column_mapping = {'A': 'X', 'B': 'Y', 'C': 'Z'}
renamed_df = df.rename(columns=column_mapping)
print(renamed_df.columns.tolist())--OUTPUT--['X', 'Y', 'Z']

The rename() method scales perfectly for changing multiple column names at once. You simply expand the dictionary to include all the desired changes, making your code organized and easy to read. This is much more efficient than calling the method multiple times.

  • Define a column_mapping dictionary where each key is an old column name and its value is the new one.
  • Pass this complete dictionary to the columns parameter to execute all renames in a single operation.

Advanced column renaming techniques

Beyond dictionary and function-based renaming, advanced approaches like regular expressions, the inplace=True parameter, and str methods offer more specialized solutions.

Renaming columns with regular expressions

import pandas as pd
import re
df = pd.DataFrame({'col_1': [1, 2], 'col_2': [3, 4], 'other': [5, 6]})
df.columns = [re.sub(r'col_(\d)', r'column_\1', col) for col in df.columns]
print(df.columns.tolist())--OUTPUT--['column_1', 'column_2', 'other']

Regular expressions give you surgical precision for renaming columns that follow a specific pattern. This example uses a list comprehension to apply Python’s re.sub() function across all column names. It’s an efficient way to perform complex, pattern-based changes in a single line.

  • The function finds names matching the pattern r'col_(\d)', which looks for "col_" followed by a digit. The parentheses capture the digit.
  • It then replaces the match with r'column_\1', where \1 inserts the captured digit into the new name.

Renaming columns in-place with inplace=True

import pandas as pd
df = pd.DataFrame({'old_name1': [1, 2], 'old_name2': [3, 4]})
print("Before:", df.columns.tolist())
df.rename(columns={'old_name1': 'new_name1', 'old_name2': 'new_name2'}, inplace=True)
print("After:", df.columns.tolist())--OUTPUT--Before: ['old_name1', 'old_name2']
After: ['new_name1', 'new_name2']

The inplace=True parameter offers a memory-efficient way to rename columns by modifying the original DataFrame directly. Instead of creating and returning a new DataFrame, this operation alters your existing data on the spot. It's a useful shortcut when you're confident you won't need the original column names again.

  • When inplace=True is used, the rename() method returns None.
  • This means you shouldn't assign the operation back to the DataFrame variable, like df = df.rename(..., inplace=True), as it will overwrite your data.

Using str methods for bulk renaming

import pandas as pd
df = pd.DataFrame({'prefix_a': [1, 2], 'prefix_b': [3, 4]})
df.columns = df.columns.str.replace('prefix_', '').str.upper()
print(df.columns.tolist())--OUTPUT--['A', 'B']

The .str accessor is a powerful tool for applying string methods across all column names at once. This approach is perfect for cleaning up headers by chaining multiple operations together in a single, readable line. You're directly modifying the column index with a series of vectorized string functions.

  • First, df.columns.str.replace('prefix_', '') removes the specified substring from each column name.
  • Next, .str.upper() is chained to convert the results to uppercase, standardizing the format.

Move faster with Replit

Replit is an AI-powered development platform that comes with all Python dependencies pre-installed, so you can skip setup and start coding instantly. This lets you move from learning individual techniques to building complete applications faster. With Agent 4, you can describe what you want to build, and it will handle the code, databases, APIs, and deployment.

Instead of piecing together methods like rename(), you can describe the final tool you need and let Agent 4 build it:

  • A CSV header cleaner that automatically converts all column names to lowercase and replaces spaces with underscores.
  • A data migration tool that renames columns based on a user-defined mapping to match a new database schema.
  • A report generator that adds a specific prefix to all columns to indicate the data source.

Simply describe your app, and Replit will write the code, test it, and fix issues automatically, all within your browser.

Common errors and challenges

Renaming columns is usually simple, but a few common issues can trip you up if you're not careful.

Fixing ValueError when column lists don't match in length

When you directly assign a new list of names to df.columns, you might encounter a ValueError. This error is a clear signal that something is wrong with the dimensions of your new names.

  • The cause: This error occurs when the number of names in the list you provide doesn't exactly match the number of columns in the DataFrame.
  • The fix: Before assigning, confirm the length of your list is correct. You can check the number of columns with len(df.columns) to ensure they match.

Resolving issues with duplicate column names

Having duplicate column names in a DataFrame can lead to ambiguity and errors, as operations may not behave as you expect. Pandas allows duplicate names, but it's a practice best avoided for clean, predictable code.

  • The problem: When you select a column with a duplicate name, pandas typically returns the first occurrence, which can hide data or cause your analysis to be incorrect.
  • The solution: Before renaming, check if any duplicates exist using the df.columns.is_unique attribute. If it returns False, you'll need to manually or programmatically make the names unique before proceeding.

Dealing with column name case sensitivity

A frequent source of errors is forgetting that column names in pandas are case-sensitive. This means 'ColumnA' and 'columna' are treated as two distinct identifiers, which can easily lead to a KeyError.

  • The trap: You might try to rename a column using the wrong case, causing a KeyError because pandas can't find the name you specified.
  • The best practice: Standardize all column names to a consistent format, such as all lowercase, using df.columns.str.lower(). This simple step eliminates case-related bugs from your workflow.

Fixing ValueError when column lists don't match in length

This ValueError is one of the most common issues when renaming columns by direct assignment. It's a strict check to prevent dimension mismatches. See the error in action below, where we try to assign a two-item list to a three-column DataFrame.

import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
# This will cause a ValueError
df.columns = ['X', 'Y']

The code triggers a ValueError because it assigns a list of two names to a three-column DataFrame. The list's length must exactly match the column count. The corrected code below shows how to resolve this common mismatch.

import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
# Ensure the number of new column names matches the DataFrame
df.columns = ['X', 'Y', 'Z']
print(df.columns.tolist())

The fix is simple: your new list of names must have the same number of items as the DataFrame has columns. The corrected code works because it provides a list with three names—['X', 'Y', 'Z']—for the three-column DataFrame. This direct assignment is an all-or-nothing operation. Always double-check your list's length against len(df.columns) before assigning to avoid this ValueError, especially when you're setting up a new dataset.

Resolving issues with duplicate column names

While pandas allows duplicate column names, this practice can introduce subtle bugs. When you select a column by a name that appears more than once, like 'X', you'll only access the first instance, which can lead to incorrect analysis.

import pandas as pd
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4], 'C': [5, 6]})
# Creating duplicate column names (allowed but problematic)
df.columns = ['X', 'X', 'Y']
# This only selects the first 'X' column
print(df['X'])

The code assigns two columns the same name, 'X'. This makes the second 'X' column inaccessible by its label, as pandas defaults to returning the first one. The code below shows how to fix this ambiguity.

import pandas as pd
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4], 'C': [5, 6]})
# Use unique column names
df.columns = ['X1', 'X2', 'Y']
# Now you can access each column distinctly
print(df[['X1', 'X2']])

The fix is to ensure every column has a unique name. By changing the names from ['X', 'X', 'Y'] to ['X1', 'X2', 'Y'], the ambiguity is resolved. This allows you to reliably access each column by its distinct label, preventing unexpected behavior in your analysis. It's wise to check for unique column names after loading or merging data, as duplicates are a common source of bugs.

Dealing with column name case sensitivity

Forgetting that pandas column names are case-sensitive is a common pitfall that leads to a KeyError. A simple mismatch, like using customerid instead of CustomerID, will cause your code to fail. The following example shows this error in action.

import pandas as pd
df = pd.DataFrame({'CustomerID': [101, 102], 'Product': [3, 4]})
# Trying to access with different case (common mistake)
try:
result = df['customerid']
except KeyError:
print("Error: Column 'customerid' not found")

The code triggers a KeyError because it attempts to access df['customerid'] when the actual column name is CustomerID. The lookup fails because pandas is case-sensitive. The code below shows how to handle this correctly.

import pandas as pd
df = pd.DataFrame({'CustomerID': [101, 102], 'Product': [3, 4]})
# Solution: Standardize case when renaming
df.columns = df.columns.str.lower()
# Now case-insensitive access works
result = df['customerid']
print(result)

The best fix for case-related KeyError exceptions is to standardize column names early. This prevents unpredictable errors during analysis. The solution shown does this effectively:

  • It applies the .str.lower() method to df.columns, converting all names to lowercase.
  • This ensures lookups like df['customerid'] work reliably, regardless of the original capitalization.

It's a great habit to get into right after loading or merging data.

Real-world applications

With the mechanics and common pitfalls covered, you can now apply these skills to solve practical, real-world data cleaning challenges through vibe coding.

Cleaning column names from imported CSV data with str methods

Data imported from CSV files often arrives with inconsistent column names, but you can easily standardize them by chaining together several str methods.

import pandas as pd
# Sample messy data with spaces and mixed case
df = pd.read_csv(pd.StringIO('First Name,Last Name,Phone #\nJohn,Doe,555-1234'))
df.columns = df.columns.str.lower().str.replace(' ', '_').str.replace('#', 'number')
print(df.columns.tolist())

This example shows how to efficiently sanitize column headers from an imported file. The key is the chained use of the .str accessor, which applies string operations to every column name at once. This vectorized approach is much cleaner than looping through the names individually.

  • First, .str.lower() standardizes the case.
  • Then, .str.replace(' ', '_') makes names valid Python identifiers.
  • Finally, another .str.replace() swaps out symbols for words.

This results in a clean, consistent set of column headers ready for analysis.

Dynamically renaming columns based on data content with apply()

You can dynamically rename columns by applying logic based on their content, such as appending each column's data type (dtype) to its name for clarity.

import pandas as pd
import numpy as np

# Dataset with different data types
df = pd.DataFrame({'col1': [1, 2, 3], 'col2': ['a', 'b', 'c'], 'col3': [1.1, 2.2, 3.3]})
# Rename columns based on data type
new_names = {col: f"{col}_{df[col].dtype}" for col in df.columns}
renamed_df = df.rename(columns=new_names)
print(renamed_df.columns.tolist())

This technique uses a dictionary comprehension to generate new column names based on their underlying data, similar to how AI-powered development can analyze and transform code patterns. It iterates through each column, checks its data type with df[col].dtype, and creates a new name by appending the type to the original.

  • The dictionary comprehension, {col: f"{col}_{df[col].dtype}" for col in df.columns}, builds the entire mapping in one line.
  • The resulting dictionary is then passed to the rename() method to apply the changes, making your column headers more descriptive.

Get started with Replit

Now, use what you've learned to build a real tool. Tell Replit Agent: "Create a utility that renames columns based on a JSON mapping file" or "Build a script that standardizes CSV headers to snake_case."

Replit Agent writes the code, tests for errors, and deploys your app, handling the entire development cycle. Start building with Replit.

Build your first app today

Describe what you want to build, and Replit Agent writes the code, handles the infrastructure, and ships it live. Go from idea to real product, all in your browser.

Build your first app today

Describe what you want to build, and Replit Agent writes the code, handles the infrastructure, and ships it live. Go from idea to real product, all in your browser.