Pandas Change Column Names – 3 Methods

Pandas Change Column names – Changing column names within pandas is easy. You only need to decide which method you want to use. Depending on your use case, you can pick the best one for you.

The easiest and most popular one will be done via the .rename() method. But look below for 2 other ways.

pandas.DataFrame.rename(columns={'old_column_name':'new_column_name'})

I use this function when I want to clean up my column names. It’s good to practice table hygiene and keep your column names short and readable.

Pseudo Code: Rename your old column name to your new column name.

Pandas Change Column Names

Pandas Change Column Names - How to change your column names in pandas with 3 methods

Method 1 – Pandas Rename

The first method that we suggest is using Pandas Rename. Rename takes a dict with a key of your old column name and a key of your new column name. Amazingly, it also takes a function!

This means that you’re able to apply a string function to your column names and apply a transformation to all of your column names. For instance if you wanted to upper case your column names.

pandas.DataFrame.rename(columns={"old_column_name" : "new_column_name"})

Method 2 – Pandas .columns attribute

The second way to rename your columns is by setting DataFrame.columns to the list of your new column names. This will completely overwrite your original columns with your new list.

Watch out though – Pandas does not know which columns you’re trying to rename. If you mistakenly mis-order your new column names you will have column names that don’t match the column contents. See below for an example

pandas.DataFrame.columns = ['your', 'new', 'column', 'names']

Method 3 – Pandas .set_axis(axis=1)

The last method (and our least favorite) is to set_axis on top of your DataFrame and specify axis=1. This will have similar functionality as setting .columns. In this case, Pandas will completely overwrite all of your column names with whatever you give it.

pandas.DataFrame.set_axis(['your', 'new', 'column', 'names'2], axis=1)

In order to grok these methods, let’s look at a couple of examples

In [2]:
import pandas as pd

Pandas Change Column Names

Let's change the names of our DataFrame's columns. We will run through 3 methods

  1. Method 1 - change column names via .rename()
  2. Method 1 - change column names via .rename() using function mapper
  3. Method 2 - change column names via .columns()
  4. Method 3 - change column names via set_axis()
In [3]:
df = pd.DataFrame([('Foreign Cinema', 'Restaurant', 289.0),
                   ('Liho Liho', 'Restaurant', 224.0),
                   ('500 Club', 'bar', 80.5),
                   ('The Square', 'bar', 25.30)],
           columns=('name', 'type', 'AvgBill')
                 )
df
Out[3]:
nametypeAvgBill
0Foreign CinemaRestaurant289.0
1Liho LihoRestaurant224.0
2500 Clubbar80.5
3The Squarebar25.3

1. Method 1 - change column names via .rename()

The most straight forward and explicit way to change your column names is via .rename(). I like this method the most because you can easily change one, or all of your column names via a dict.

Here I'm going to change the column name 'AvgBill' to 'Bill'. You need ot tell pandas that you want to change your columns so you'll need to specify axis=1 or columns=your_mapper. I marginally prefer setting 'columns=' because I don't need to remember if columns is axis=1 or 0.

A mapper is a fancy word for 'What do you want to convert your old values into?'

In [8]:
df.rename({'AvgBill' : 'Bill'}, axis=1)
Out[8]:
nametypeBill
0Foreign CinemaRestaurant289.0
1Liho LihoRestaurant224.0
2500 Clubbar80.5
3The Squarebar25.3
In [9]:
df.rename(columns={'AvgBill' : 'Bill'})
Out[9]:
nametypeBill
0Foreign CinemaRestaurant289.0
1Liho LihoRestaurant224.0
2500 Clubbar80.5
3The Squarebar25.3

2. Method 1 - change column names via .rename() using function mapper

Another beautiful thing you can do with rename is pass a function as a mapper. Pandas will apply the function to the column names.

In this case I'm going to upper each of my column names.

Then I'm going to remove the first letter from each of my column names.

In [15]:
df.rename(columns=str.upper)
Out[15]:
NAMETYPEAVGBILL
0Foreign CinemaRestaurant289.0
1Liho LihoRestaurant224.0
2500 Clubbar80.5
3The Squarebar25.3
In [16]:
df.rename(columns=lambda x: x[1:])
Out[16]:
ameypevgBill
0Foreign CinemaRestaurant289.0
1Liho LihoRestaurant224.0
2500 Clubbar80.5
3The Squarebar25.3

3. Method 2 - change column names via .columns()

The next way to change your column names is by setting them all specifically via a list. This is slightly more verbose because you need to outline all of your column names, not just the ones you want to change.

Also, you're at a higher risk of mislabeling column names if you accidentally get your orders confused. Be careful with this one.

In [19]:
df.columns = ['Name', 'type', 'ABill']
df
Out[19]:
NametypeABill
0Foreign CinemaRestaurant289.0
1Liho LihoRestaurant224.0
2500 Clubbar80.5
3The Squarebar25.3
In [20]:
df.columns = ['Bill', 'name', 'type']
df
Out[20]:
Billnametype
0Foreign CinemaRestaurant289.0
1Liho LihoRestaurant224.0
2500 Clubbar80.5
3The Squarebar25.3

4. Method 3 - change column names via set_axis()

Lastly, you could also change your column names by setting your axis. This last method is great, but doesn't have many advantages (if any) over the first two.

In [21]:
df.set_axis(["Name", "Type", "AvgBill"], axis=1)
Out[21]:
NameTypeAvgBill
0Foreign CinemaRestaurant289.0
1Liho LihoRestaurant224.0
2500 Clubbar80.5
3The Squarebar25.3
In [22]:
df.set_axis(["Name", "Type", "Bills"], axis=1)
Out[22]:
NameTypeBills
0Foreign CinemaRestaurant289.0
1Liho LihoRestaurant224.0
2500 Clubbar80.5
3The Squarebar25.3

Link to code above

Check out more Pandas functions on our Pandas Page

Official Documentation