Renaming Columns In A Pandas DataFrame

Python’s rename column is a method used to change the column nambes with Pandas’ rename function. It’s useful when we load a tabular dataset that has no column names or if we want to assign different names to specific columns.


Its syntax is given as:

Where d is a dictionary, & the keys are the columns we want to change. The values are the new names for these columns. The code inplace = False means the result would be stored in a new DataFrame instead of the original one.

Renaming Columns

We have the following dataset called df2_melted.

With this dataset, we want to do the following:

  • Import pandas as pd
  • Rename the variable column of df2_melted to Year & the value column to Income & assign it to df2_tidy
  • Finally, print the updated dataframe

When we run the above code, it produces the following result:


Data Scientist & Machine Learning Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store