How to rename the columns of a DataFrame

1 min readDataFramePandasGetting StartedData
7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

There is the .columns attribute given by the Pandas library that will give you the current columns of a DataFrame.

You might end up needing to change them.

To do so, you will need to assign a list of new column names to the DataFrame.columns as so:

# we assign the new columns to the dataframe.columns attribute
df.columns = ["col1","col2","col3"]
How to rename the columns of a DataFrame

Be careful to match the correct number of columns that your DataFrame currently have or you will run into an error.

Renaming the index column

In order to rename the index column you will assign a new value to the index.name attribute as so:

df.index.name = "my_new_index_column_name"
How to rename the index column of a DataFrame

Here you are ! You know how to rename both columns and index columns of a DataFrame.

7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Free Newsletter

Master Data Science in Days, Not Months 🚀

Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚

Weekly simple and practical lessons
Access to ready to use code examples
Skip the math, focus on results
Learn while drinking your coffee

By subscribing, you agree to receive our newsletter. You can unsubscribe at any time.