How to rename a column if it exists in a pandas DataFrame?

Step 1: Create your Example DataFrame

First we will create a simple DataFrame object and assign a column index to it.

import pandas as pd

df_cols = ['Office', 'Language', 'Sales', 'Expenses']
sales_df = pd.DataFrame(columns = df_cols )

Note: make sure to define the columns index when creating the DataFrame. Trying to assign the column index to the DataFrame after creating it will cause the following exception:

#ValueError: Length mismatch: Expected axis has 0 elements, new values have 4 elements

Step 2: Check if columns exists

We can easily check if one or multiple columns exists in the column index. Let’s assume that we would like to find out whether the DataFrame has a Sales column.

col_name  = 'Sales'
col_name in sales_df.columns

This will return the boolean value True.

Step 3: Rename columns that exists

We can now use an IF statement and change the name of a column assuming it is part of the column index.

# old and new names
col_name  = 'Sales'
new_col_name = 'ARR'

if col_name in sales_df.columns:
    sales_df.rename(columns = {col_name:new_col_name}, inplace=True)
    print (f'Column {col_name} was renamed to {new_col_name}')
    print(f'Column {col_name} wasn\'t found in the DataFrame')

This will return the following result:

Column Sales was renamed to ARR

Important: Note the usage of the inplace=True parameter to persist the column naming in your DataFrame . You can also assign the DataFrame with the renamed columns into a new object named new_sales_df to persist your changes without modifying the original DataFrame.

if col_name in sales_df.columns:
    new_sales_df = sales_df.rename(columns = {col_name:new_col_name})
    print (f'Column {col_name} was renamed to {new_col_name}')
    print(f'Column {col_name} wasn\'t found in the DataFrame')

Renaming multiple columns in pandas DataFrame

For completeness, here’s the code you need to alter the names of several columns in your column index. Also here we use a dictionary object to pass the existing and new column names, this time it will be for multiple columns.

sales_df.rename(columns = {'Sales':'ARR', 'Expenses':'COGS'}, inplace=True)

Additional Learning

How to change column names in your DataFrame if not working?