Rename Column Name(s) in Pandas DataFrame

Rename Column Name(s) in Pandas DataFrame

When working with datasets from external sources - column names can get wild. Different naming conventions, cases (snake_case, CamelCase, etc.), as well as names are common. A common headache is caused by really long column names, that you might have to call on many times in the lifecycle of data manipulation.

One of the first steps in many preprocessing pipelines is renaming the columns.

Let's create a simple mock DataFrame with a couple of columns:

import pandas as pd

df = pd.DataFrame({'Short col name': [1], 
                  'Really long column name': [2],
                   'col3': [3]})
                   
print(df)

It has three columns with one row each:

Short col name  Really long column name  col3
0               1                        2     3

Rename Specific Columns

To rename the columns, you call the DataFrame.rename() method, providing the columns argument with a dictionary of {'old name' : 'new name'}:

df = df.rename(columns={'Short col name': 'col1', 'Really long column name': 'col2'})

print(df)

This results in:

   col1  col2  col3
0     1     2     3

Get free courses, guided projects, and more

No spam ever. Unsubscribe anytime. Read our Privacy Policy.

Note: You don't need to provide every column here - we've totally skipped col3 because it already follows our mock convention. Only the columns you reference will be found and renamed.

This operation is not done in-place, so you'll want to assign the result of the method to a new DataFrame instance or the object already in memory as we have. To perform the operation in-place, add the inplace flag:

df.rename(inplace=True, columns={'Short col name': 'col1', 'Really long column name': 'col2'})
print(df)

This results in:

  col1  col2  col3
0     1     2     3

Rename All Columns

Another way to rename the columns is to, well, replace the columns (list of column names) by pointing it to another array:

df.columns = ['col_1', 'col_2', 'col_3']
print(df)

This results in:

   col_1  col_2  col_3
0      1      2      3
Last Updated: July 5th, 2022
Was this helpful?
David LandupAuthor

Entrepreneur, Software and Machine Learning Engineer, with a deep fascination towards the application of Computation and Deep Learning in Life Sciences (Bioinformatics, Drug Discovery, Genomics), Neuroscience (Computational Neuroscience), robotics and BCIs.

Great passion for accessible education and promotion of reason, science, humanism, and progress.

Course

Data Visualization in Python with Matplotlib and Pandas

# python# pandas# matplotlib

Data Visualization in Python with Matplotlib and Pandas is a course designed to take absolute beginners to Pandas and Matplotlib, with basic Python knowledge, and...

David Landup
David Landup
Details
Project

Data Visualization in Python: The Collatz Conjecture

# python# matplotlib# data visualization

The Collatz Conjecture is a notorious conjecture in mathematics. A conjecture is a conclusion based on existing evidence - however, a conjecture cannot be proven....

David Landup
Jovana Ninkovic
Details

© 2013-2022 Stack Abuse. All rights reserved.

DisclosurePrivacyTerms