Python

Pandas rename(): How to Rename Columns in Pandas Dataframe

By ayed_amira , on 08/19/2021 - 4 minutes to read
pandas rename

Pandas rename(): In this tutorial we will see how to use the rename() function of the Pandas library and other methods to rename one or more columns of a dataframe.

Introduction

A pandas dataframe is a two-dimensional tabular data structure that can be modified in size with labeled axes that are commonly referred to as row and column labels, with different arithmetic operations aligned with the row and column labels.

The Pandas library, available on python, allows to import data and to make quick analysis on loaded data.

When manipulating data, it may be necessary to modify the names of specific columns in a dataframe. This allows you to correct a spelling mistake, for example, or to give more comprehensive column names. We will see that the rename() function is very useful for this kind of situation but it is not the only option.

In this tutorial we will discuss different ways of renaming column names:

  • Rename a single column using rename()
  • Rename multiple column using rename()
  • Use set_axis() to change all your column names
  • Set the dataframe’s columns attribute

To explain these different points, we will use the following pandas dataframe:

import pandas as pd

website = [("google", 20409, 40900, 61000),
           ("amiradata", 18389, 32300, 55000),
           ("amazon", 18090, 19303, 23000),
           ("yahoo", 10045, 11354, 12000),
           ("facebook", 7309, 8200, 15000)
           ]

df = pd.DataFrame(website, columns=['site','nbu', 'nbs', 'nbpv'])

print(df)

Output:

        site    nbu    nbs   nbpv
0     google  20409  40900  61000
1  amiradata  18389  32300  55000
2     amazon  18090  19303  23000
3      yahoo  10045  11354  12000
4   facebook   7309   8200  15000

Pandas Rename

Pandas Rename() – Syntax and parameters

Syntax

# Syntax

DataFrame.rename(mapper=None,
index=None,
columns=None,
axis=None,
copy=True,
inplace=False,
level=None,
errors='ignore')

Parameters

The rename() function can take 8 parameters:

Name Description Type Default Value Required
mapperDict-like or function transformations to apply to that axis’ values.dict-like or functionNoneYes
indexAlternative to specifying axisdict-like or functionNoneNo
columnsAlternative to specifying axisdict-like or functionNoneNo
axisAxis to target with mapper{0 or ‘index’, 1 or ‘columns’}0No
copyAlso copy underlying data.booleanTrueNo
inplaceWhether to return a new DataFrame. If True then value of copy is ignored.booleanFalseNo
levelIn case of a MultiIndex, only rename labels in the specified level.int or level nameNoneNo
errorsIf ‘raise’, raise a KeyError
 when a dict-like mapper,index,or columns
 contains labels that are not present in the Index being transformed. If ‘ignore’, existing keys will be renamed and extra keys will be ignored.
{‘ignore’, ‘raise’}ignoreNo

Pandas Rename a single column using rename()

To rename a single column, we can use the pandas rename() function. This function is very useful because it is not necessary to create a new column to rename the column name we want to change.

So we will try to change the column ‘nbu’ to ‘nb_users’ in our example dataframe:

# Rename Single Column

df.rename(columns = {'nbu':'nb_users'}, inplace = True)
print(df.columns)

Output:

Index(['site', 'nbu', 'nbs', 'nbpv'], dtype='object')

To avoid creating a new dataframe, we can use the inplace parameter which will modify our dataframe directly

Pandas Rename multiple column using rename()

It is possible to rename multiple columns directly in the rename() function:

# Rename Multiple Column

df.rename(columns = {'nbu':'nb_users','nbs':'nb_sessions','nbpv':'nb_pageviews'}, inplace = True)
print(df.columns)

Output:

Index(['site', 'nb_users', 'nb_sessions', 'nb_pageviews'], dtype='object')

Use set_axis() to change all your column names

We could also change the column names by defining the axis. It is not necessary to specify the current column names in the function :

# set_axis() method

df.set_axis(['website', 'nb_users', 'nb_sessions', 'nb_pageviews'], axis=1,inplace=True)
print(df.columns)

Output:

Index(['website', 'nb_users', 'nb_sessions', 'nb_pageviews'], dtype='object')

Set the dataframe’s columns attribute

You can also update the dataframe’s column by setting its columns attribute to your new list of columns :

# df.columns 

df.columns = ['website', 'nb_users', 'nb_sessions', 'nb_pageviews']
print(df.columns)

Output:

Index(['website', 'nb_users', 'nb_sessions', 'nb_pageviews'], dtype='object')
Note : Your new list must have the same length as the number of columns of the current dataframe.

Conclusion

In this tutorial, we explore the different methods to rename one or more columns of a pandas dataframe. As I said at the beginning, this is an important step in the data analysis. It allows us to better manipulate the data and understand what information is available in our dataframe.

Don’t hesitate if you have any questions on the subject, I will be happy to answer them.

See you soon !

Back to Python Menu

ayed_amira

I'm a data scientist. Passionate about new technologies and programming I created this website mainly for people who want to learn more about data science and programming :)

Comments

Leave a comment

Your comment will be revised by the site if needed.