Guides
Guide to renaming columns with Python Pandas
One of the most common actions while cleaning data or doing exploratory data analysis (EDA) is manipulating/fixing/renaming column names. So in this post, we will explore various methods of renaming columns of a Pandas dataframe. Two ways of modifying column titles
There are two main ways of altering column titles:
1.) the columns method and
2.) the rename method.Columns methodIf we have our labelled DataFrame already created, the simplest method for overwriting the column labels is to call the columns method on the DataFrame object and provide the new list of names we’d like to specify.
For example, if we take our original DataFrame:We can modify the column titles/labels by adding the following line:df.columns = ['Column_title_1','Column_title_2']
A problem with this technique of renaming columns is that one has to change names of all the columns in the Dataframe. This approach would not work if we want to change the name of just one column. The rename method outlined below is more versatile and works for renaming all columns or just specific ones.Rename methodThe other technique for renaming columns is to call the rename method on the DataFrame object, than pass our list of labelled values to the columns parameter:df.rename(columns={0 : 'Title_1', 1 : 'Title2'}, inplace=True)
Its important to note that since the rename method is attempting to actually rename existing labels, you need to specify the existing label first, followed by the new label. As shown in the example above.
Please note, we specify the True
value for the inplace
parameter here, in order to update the existing DataFrame. Whitout this, the function call returns a newly created DataFrame instead.Change a single column header nameIf you need to rename a specific column you can use the df.rename()
function and refer the columns to be renamed. Not all the columns have to be renamed when using rename method:df.rename(columns={'old_column_name': 'new_column_name'}, inplace=True)
)
Continue Reading
Apps
Timestripe - my new favourite productivity app
March 5, 2023
Guides
How to scrape tables from websites using Pandas read_html() function
February 2, 2023
Guides
Drop all duplicate rows across multiple columns in Python Pandas
January 28, 2023
Guides
How to create effective prompts for AI image generation
August 15, 2022
Guides
Generate Huge Datasets With Fake Data Easily and Quickly using Python and Faker
April 16, 2022
Guides
How to change or update a specific cell in Python Pandas Dataframe
March 25, 2021
Guides
How to add a row at the top in Pandas dataframe
March 22, 2021
Guides
Creating WordClouds in Python from a single-column in Pandas dataframe
November 15, 2020
Guides
Python Regex examples - How to use Regex with Pandas
September 9, 2020
Guides
Python regular expressions (RegEx) simple yet complete guide for beginners
September 15, 2020
Guides
8 Python Pandas Value_counts() tricks that make your work more efficient
May 31, 2020
Guides
Exploring Correlation in Python: Pandas, SciPy
May 5, 2020
Guides
How to add new columns to Pandas dataframe?
March 22, 2020
Guides
Delete column/row from a Pandas dataframe using .drop() method
February 2, 2020
Guides
How to visualize data with Matplotlib from a Pandas Dataframe
November 15, 2019
Guides
The ultimate beginners guide to Group by in Python Pandas
August 8, 2019
Guides
How to suppress scientific notation in Pandas
July 12, 2019
Guides
The complete beginners guide to Pandas
June 29, 2019
Guides
Data project #1: Stockmarket analysis
June 29, 2019
Guides
Use Jupyter notebooks anywhere
June 10, 2019