9 tactics to rename columns in pandas dataframe

Rename columns in pandas dataframe is a very basic operation when it comes to Data Wrangling. In this article I am going to cover 9 different tactics for renaming columns using pandas library. Some of these could be unknown to many aspiring Data Scientists.

Following are few tactics ways which I am going to cover in the article.

  • Renaming specific columns with rename method
  • Using axis parameter with pandas rename method
  • Rename pandas columns using set_axis method
  • Assign list of columns to .columns attribute of dataframe
  • Renaming all columns with a lambda function
  • Adding prefix and suffix to the column
  • Rename columns using regular expressions
  • Rename MultiIndex columns in Pandas
  • Converting MultiIndex columns to single level

Before we move ahead, let’s import required libraries.

Rename columns in pandas using a map

First prepare a map or dictionary object with mapping of old column names as key and new column names as values. Using pandas rename function pass above created map as an argument.

Rename columns using axis parameter

From pandas release 0.21 onward you can set axis parameter to 1 or columns to rename pandas columns. We don’t have to use columns parameter at all.

Assign list of columns to .columns attribute of dataframe

We can simply assign new column name list to df.columns. Consider following points while using this method.

  • You must pass new column list with same length as total number of columns in your dataframe
  • Input list of new column names must be in same sequence as your existing column sequence
  • It is convenient when you want to do some cleanup or do minor changes in your existing column names

Rename pandas columns using set_axis method

Using set_axis method is a bit tricky for renaming columns in pandas. Same as above example, you can only use this method if you want to rename all columns. You cannot use inplace=True to update the existing dataframe.

In following example, we are trying to modify existing columns.

Why one should use set_axis method to rename pandas columns?

Primarily set_axis() method is convenient with chain modifications. Let’s understand this concept with following example.

In pandas we can chain transformations in following way.

output_df = df.transformation1().transformation2().transformation3()

Now what if someone wants rename dataframe columns after transformation2()? We have to store result of transformation2() in some dataframe, rename dataframe columns and than execute transformation3()

set_axis() method allows to continue the transformation chain as following.

Renaming all columns with a lambda function

For updating all columns with some complicated logic, we can use lambda function directly with rename method.

Adding prefix and suffix to the column

Pandas dataframe has two separate methods for adding prefix and suffix to all columns.

Rename columns using regular expressions

With a sample dataframe in which each column is a date string in YYYY-MM-DD format and we want to convert all columns to string in DD-MM-YYYY format. Above conversion can easily be done using regex in python.

Rename MultiIndex columns in Pandas

To rename multiindex columns in pandas level by level, set_levels method of DataFrame.columns can be used.

CompanyBMWAudi
ModelX7X5X3Q7Q5Q3
2019513628444433119290816172296810
2018269479356434155510998570056759
2017701475959491484780856730595420
CompanyManufacturer: BMWManufacturer: Audi
ModelModel: X7Model: X5Model: X3Model: Q7Model: Q5Model: Q3
2019814478671272330638794601731607
2018364847556965724444386005863423
2017714888325721159387783096278200

Converting MultiIndex columns to single level

BMW X7BMW X5BMW X3Audi Q7Audi Q5Audi Q3
2019757635020460707480309777642762
2018516848457138951609714434964299
2017953564902289497444204021654111

So ultimately we covered all different tactics to rename column in pandas dataframe. I hope these will help you in your journey from beginner to an expert in Data Wrangling.

Stay tuned for more awesome posts..!! Happy Learning..!!!

Leave a Reply

avatar
  Subscribe  
Notify of