Renaming columns in a Pandas DataFrame | by B. Chen | Towards Data Science

In data analysis, we may work on a dataset that has no column names or column names contain some unwanted characters (e.g. space), or maybe we just want to rename columns to have better names. These all require us to rename columns in a Pandas DataFrame.
In this article, you’ll learn 5 different approaches to do that. This article is structured as follows:
Passing a list of names to columns attributeUsing rename() functionRenaming columns while reading a CSV fileUsing columns.str.replace() methodRenaming columns via set_axis()
Passing a list of names to columns attribute
Using rename() function
Renaming columns while reading a CSV file
Using columns.str.replace() method
Renaming columns via set_axis()
For demonstration, we will be using a subset from the Titanic dataset available on Kaggle.
def load_data():     df_all = pd.read_csv('data/titanic/train.csv')    # Take a subset    return df_all.loc[:, ['PassengerId', 'Pclass', 'Name', 'Sex']]df = load_data()
Please check out Notebook for the source code.
Pandas DataFrame provides a columns attribute for us to access the column names. The column name can also be renamed by directly passing a list of new names to the columns attribute.
# Access column names>>> df.columnsIndex(['PassengerId', 'Pclass', 'Name', 'Sex'], dtype='object')# Rename PassengerId to Id, Pclass to Class>>> df.columns = ['Id', 'Class', 'Name', 'Sex']
However, a disadvantage with this approach is that we need to provide names for all columns even if we want to rename only some of them. Otherwise, we will get a “ValueError: Length mismatch”:
Pandas has a built-in function called rename() to change the column names. It’s useful when you want to rename some selected columns.
To rename columns, we can pass a dictionary to the columns argument. The keys are the columns you want to change and the values are the new names for these columns. We can also set the argument inplace to True for the change to happen in the existing DataFrame.
# Rename columns#   PassengerId  ->  Id#   Pclass       ->  Classdf.rename(    columns=({ 'PassengerId': 'Id', 'Pclass': 'Class'}),     inplace=True,)df.head()
Instead of a dictionary mapping, we can also pass a function to the columns argument. For example, to convert column names into lowercase, we can pass a str.lower function:
df.rename(columns=str.lower).head()
We can also create a custom function and pass it to the columns argument.
def toUpperCase(string):    return string.upper()df.rename(columns=toUpperCase).head()
We can also use lambda expression:
df.rename(columns=lambda s: s.upper()).head()
This is useful when you need to update many columns or all columns with the same naming convention.
Turns out rename() can be used to rename index as well. Let’s first create a DataFrame:
df = pd.DataFrame(    { "ID": [1, 2], "City": ['London', 'Oxford']},     index=['A1', 'A2'],)
We can use dictionary mapping to rename index:
df.rename({ 'A1': 'B1', 'A2': 'B2' })
We can also pass a function and set the axis argument to 0 or 'index'
df.rename(str.lower, axis=0)
We can actually rename the columns while reading a CSV file. Here is what we can do to override the column names:
new_names = ['ID', 'Survived', 'Class', 'Name', 'Sex']df = pd.read_csv(    'data/titanic/train.csv',     names=new_names,           # Rename columns    header=0,                  # Drop the existing header row    usecols=[0,1,2,3,4],       # Read the first 5 columns)
We created a list of column names new_names and pass it to the names argument. The CSV file already contains a header row, therefore the argument header=0 is needed to override the column names.
If you want to learn more about read_csv(), please check out the below article.
towardsdatascience.com
There is a method called columns.str.replace() [1] allow us to replaces a specified phrase with another specified phrase, for example, replace space with an underscore.
df = pd.DataFrame(    { "account id": [1, 2], "uk city": ['London', 'Oxford']},     index=['A1', 'A2'],)df.columns = df.columns.str.replace(' ', '_')
This method is also available to index via df.index.str.replace().
Lastly, we could also change column names by setting the axis. This method is great, but it doesn’t many advantages over the first two.
df.set_axis(['ID', 'Survived', 'Class', 'Name', 'Sex'], axis=1)
This article shows 5 different approaches to rename columns in Pandas DataFrame. Here is a summary:
the columns attribute is the easiest but it requires us to provide names for all columns even if we want to rename only some of them. This approach is sensible when working on a DataFrame with just a couple of columns.
When reading a CSV file, it may be more sensible to rename columns using read_csv() with names argument.
When you want to rename some selected columns, the rename() function is the best choice.
columns.str.replace() is useful only when you want to replace characters. Note, passing a custom function to rename() can do the same.
Lastly, we could also change column names by setting axis via set_axis().
I hope this article will help you to save time in learning Pandas. I recommend you to check out the Pandas documentation for those APIs and to know about other things you can do.
Thanks for reading. Please check out the notebook for the source code and stay tuned if you are interested in the practical aspect of machine learning.
All Pandas json_normalize() you should know for flattening JSON
A Practical Introduction to Pandas Series
Using Pandas method chaining to improve code readability
How to do a Custom Sort on Pandas DataFrame
All the Pandas shift() you should know for data analysis
When to use Pandas transform() function
Pandas concat() tricks you should know
Difference between apply() and transform() in Pandas
All the Pandas merge() you should know
Working with datetime in Pandas DataFrame
Pandas read_csv() tricks you should know
4 tricks you should know to parse date columns with Pandas read_csv()
More tutorials can be found on my Github
Reference
[1] Data School: how do I rename columns in a Pandas DataFrame