Introduction
Renaming dataframe columns is a common practise especially when we are interested in sharing some insights with other people and teams. This means that we may wish to make column names more meaningful so that it would be easier for readers to relate them to specific contexts.
In this short article, we will look at a few of options we have when it comes down to renaming columns of pandas DataFrames. Specifically, we are going to see how to rename columns:
- using the
rename()
method - by updating the
DataFrame.columns
attribute - and using
set_axis()
method
First, let’s create an example DataFrame that will reference throughout this guide in order to showcase the desired pandas functionality.
import pandas as pd
df = pd.DataFrame({
'colA':[1, 2, 3],
'colB': ['a', 'b', 'c'],
})
print(df)
# colA colB
# 0 1 a
# 1 2 b
# 2 3 c
Using .rename()
[pandas.DataFrame.rename()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.rename.html)
can be used to alter columns’ or index name.
Alter axes labels.
Function / dict values must be unique (1-to-1). Labels not contained in a
dict
/Series
will be left as-is.
In order to rename columns using rename()
method, we need to provide a mapping (i.e. a dictionary) where keys are the old column name(s) and values are the new one(s). Additionally, we must specify axis=1
in order to denote that we wish to rename columns and not the index:
df = df.rename({'colA': 'A', 'colB': 'B'}, axis=1)
print(df)
# A B
# 0 1 a
# 1 2 b
# 2 3 c
Updating df.columns attribute
pandas DataFrames come with [pandas.DataFrames.columns](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.columns.html)
attribute which is an Index
containing the column labels of the DataFrame.
We can rename DataFrame columns by re-assigning this particular attribute as shown below:
df.columns = ['column_A', 'column_B']
print(df)
# column_A column_B
# 0 1 a
# 1 2 b
# 2 3 c
Using set_axis()
[pandas.DataFrame.set_axis()](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.set_axis.html)
method can be used to assign a desired index to either the column or index axis. In order to rename the column names, make sure to provide axis=1
as shown below:
df = df.set_axis(['AA', 'BB'], axis=1, inplace=False)
print(df)
# AA BB
# 0 1 a
# 1 2 b
# 2 3 c
Note that in all of the examples discussed earlier, you can even use axis='columns'
instead of axis=1
in order to denote that the operation should be effective on the column level. For example,
df = df.rename({'colA': 'A', 'colB': 'B'}, axis='columns')
df = df.set_axis(['AA', 'BB'], axis='columns')
Final Thoughts
In today’s short guide we discussed how to rename columns of pandas DataFrames in a few different ways.
You may also be interested in understanding how to change the data types of specific columns of pandas DataFrames.
Additionally, the article below discusses how to perform proper row selection based on specific conditions.
How To Select Rows From Pandas DataFrame Based on Column Values