How to Rename Columns in Pandas - A Quick Guide | Towards Data Science

Ensuring that dataframe columns are appropriately named is essential to understand what data is contained within, especially when we pass our data on to others. In this short article, we will cover a number of ways to rename columns within a pandas dataframe.
But first, what is Pandas? Pandas is a powerful, fast, and commonly used python library for carrying out data analytics. The Pandas name itself stands for “Python Data Analysis Library”. According to Wikipedia, the name originates from the term “panel data”. It allows data to be loaded in from a number file formats (CSV, XLS, XLSX, Pickle, etc.) and stored within table-like structures. These tables (dataframes) can be manipulated, analyzed, and visualized using a variety of functions that are available within pandas
The first steps involve importing the pandas library and creating some dummy data that we can use to illustrate the process of column renaming.
import pandas as pd
We will create some dummy data to illustrate the various techniques. We can do this by calling upon the .DataFrame() Here we will create three columns with the names A, B, and C.
df = pd.DataFrame({'A':[1,2,3,4,5],                    'B':[101,102,103,104,105],                  'C':[42,42,42,42,42]})
An alternative method for creating the dataframe would be to load the data from an existing file, such as a csv or xlsx file. When we load the data, we can change the names of the columns using the names argument. When we do this, we need to make sure we drop the existing header row by using header=0.
df = pd.read_csv('data.csv',                      names=['ColA', 'ColB', 'ColC'],                     header=0)
The first method we will look at is the .rename() function. Here we can pass in a dictionary to the columns keyword argument. The dictionary allows us to provide a mapping between the old column name and the new one that we want.
We will also set the inplaceargument to True so that we are making the changes to the dataframe, df, directly as opposed to making a copy of it.
df.rename(columns= {'A':'Z', 'B':'Y', 'C':'X' }, inplace=True)
An alternative version of this is to specify the axis, however, it is less readable and may not be clear what this argument is doing compared to using the columns argument.
df.rename({'A':'Z', 'B':'Y', 'C':'X' }, inplace=True, axis=1)
When we call upon df, we now see that our columns have been renamed from A, B, and C to Z, Y, and X respectively.
If we want to rename specific columns, we can use the rename function again. Instead of providing a string for string mapping, we can use df.columns and select a column by providing a column index position in the square brackets. We then map this to a new column name string.
df.rename(columns={df.columns[0]:'New Name'}, inplace=True)
We can also specify a mapping between an existing column name and the new one.
df.rename(columns= {'X':'XX' }, inplace=True)
The next method is set_axis() which is used to set the axis (column: axis=1 or row: axis=0) of a dataframe.
We can use this method to rename the columns by first defining our list of names we want to replace the columns with and setting axis=1 or axis='columns'. Note that here the number of names needs to equal the total number of columns.
df.set_axis(['Column A', 'Column B', 'Column C'],             axis='columns',             inplace=True)
We can rename the columns directly by assigning a new list containing the names that we want to rename the columns to. This is achieved using the df.columns attribute of the dataframe.
This method requires the new list of names to be the same length as the number of columns in the dataframe. Therefore, if we only want to rename one or two columns this is probably not the best approach.
df.columns = ['1', '2', '3']
The final method we will look at is using str.replace(), which can be used to replace specific characters or entire column names.
In this example, we will replace column 1 with the letter Z.
df.columns = df.columns.str.replace('1', 'Z')
There are multiple methods for renaming columns within a pandas dataframe including pd.read_csv, .set_axis, df.rename and df.columns. This illustrates the great flexibility that is available within the pandas python library and makes it easy to ensure that columns within a dataframe are appropriately labeled.
Thanks for reading!
If you have found this article useful, feel free to check out my other articles looking at various aspects of Python and well log data. You can also find my code used in this article and others at GitHub.
If you want to get in touch you can find me on LinkedIn or at my website.
Interested in learning more about python, petrophysics, or well log data petrophysics? Follow me on Medium or on YouTube.