How to change column names in Python?

In today’s quick tutorial we’ll learn how to modify column names in Python Pandas dataframes.

Consider the following very simple Pandas DataFrame that we are going to use in our examples.

Let us look into the columns names.

Index(['DSCITY', 'MNTP', 'MXTP'], dtype='object')

As you can see the column names have technical names which we might want to simplify so that others working with our data will better understand.

Change one or multiple column names with DataFrame.rename()

The Pandas DF rename method allows us to rename one or multiple columns. All we need is to pass a dictionary mapper that matches between the old columns names to the old ones.

Here we go:

# Modifying header names in Python
weather.rename(columns= {'DSCITY': 'city', \
                         'MNTP': 'min_temp', \
                         'MXTP':'max_temp'}, \


  • You can pass one or more key – value pairs corresponding to one or multiple headings to rename.
  • The inplace=True parameter, persists your changes to the DataFrame headers. The default value is false (no need to specifically specify that when calling the DataFrame.rename() method).

Rename columns when importing from csv with pd.read_csv()

You can also rename headers when acquiring your data from an external file or API.

Let’s look at the pd.read_csv example, that allows to acquire data from csv, tsv and text files. We’ll first define a new list of columns names and then once importing the file, use the names parameter to define the new header names.

' Define a list of new heading names
col_names = ['city', 'min_temp', 'max_temp']
'Read the Dataframe
pd.read_csv('../Data/weather.csv', names= col_names)

Renaming the Pandas Index

We can replace not only the header values but also the index one. Here’s a very simple example:

weather.rename(index= {1:'a', 2:'b', 3:'c'})

Leave a Comment