So you want to rename a column in a Python Pandas dataframe. Is that possible? Yes, it is. You use the rename()
method to rename an individual column or the “columns” attribute to assign a new set of column headers to a dataframe.
In this guide, we cover how to rename an individual column and multiple columns in a Pandas dataframe. We walk through two examples to help you get started with these techniques.
Rename a Single Column in Pandas
A Pandas dataframe is a grid that stores data. Data is stored in a table using rows and columns. Each axis in a dataframe has its own label.
You rename a single column using the rename()
function. This method is useful because it lets you modify a column heading without having to create a new column.
Take a look at a Pandas dataframe object:
import pandas as pd books = { "name": ["The Great Gatsby", "To Kill a Mockingbird", "The Count of Monte Cristo"], "author": ["F. Scott Fitzgerald", "Harper Lee", "Alexandre Dumas"], "sold": [42, 53, 39] } books_frame = pd.DataFrame(books)
“name”, “author”, and “sold” are our column headings. This dataframe has three columns and three rows. We see our dataframe by printing it out to the console:
print(books_frame)
Our dataframe appears as expected:
name | author | sold | |
0 | The Great Gatsby | F. Scott Fitzgerald | 42 |
1 | To Kill a Mockingbird | Harper Lee | 53 |
2 | The Count of Monte Cristo | Alexandre Dumas | 39 |
Next we rename the column “sold” to say “copies sold”. We can do this using the rename()
method:
books_frame.rename(columns = {"sold": "copies sold"}, inplace=True)
We specify one parameter with the rename()
method: columns. This parameter accepts a list of columns to rename.
We also specify a dictionary as the value of the columns parameter. “sold” refers to the name of the column we want to rename. “copies sold” refers to the name with which we want to replace the old name.
Let’s run our code and print out our list of columns to the console:
books_frame.rename(columns = {"copies": "copies sold"}, inplace=True) print(books_frame.columns)
Our code returns:
Index(['name', 'author', 'copies sold'], dtype='object')
The “sold” column has been renamed to “copies sold”.
Rename Multiple Columns in Pandas
You use the rename()
method to rename multiple columns. You do this by specifying multiple column values in the dictionary assigned to the “columns” parameter.
Let’s change the “name” header to say “Book Name” and “sold” to say “Copies Sold”:
books_frame.rename(columns= { "sold": "Book Name", "name": "Book Name" }, inplace=True) print(books_frame.columns)
The rename()
method renames our columns. Our code returns:
Index(['Book Name', 'author', 'Book Name'], dtype='object') "name" and "sold" are renamed. "author" remains the same.
Rename All Columns in Pandas
You rename all the columns in a Pandas dataframe by assigning the “columns” attribute a list of new column headings. This approach only works if you want to rename every column in a table; you cannot exclude columns whose names should stay the same.
We overhaul our column headings from the last example:
- “name” should become “Book Title”
- “author” should become “Author Name”
- “copies” should become “Number of Copies Sold”
Make these changes to our dataframe. We first create a list with the new column names and assign it to the “columns” attribute variable:
books_frame.columns = ["Book Title", "Author Name", "Number of Copies Sold"] print(books_frame.columns)
Our code returns:
Index(['Book Title', 'Author Name', 'Number of Copies Sold'], dtype='object')
Our code has renamed all our columns.
Conclusion
The rename()
method allows you to rename one or more column names in Pandas. You do so in a Pandas dataframe by reassigning the value of the “columns” attribute.
Now you’re ready to rename columns in Pandas like an expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.