Pandas Rename column names – How to

python pandas rename column

Using pandas and need to rename some columns? Here are a few different ways you can rename your columns in pandas, depending on how complicated your data frame is, whether the column names are part of your data frame or not and whether you need to rename the same column multiple times. The examples below all use Python 3, but the methods apply to any modern version of python (2 or 3).

How to rename column in Pandas Dataframe

Create a Sample DataFrame

import pandas as pd
# Create dataframe pandas with column names
df = pd.DataFrame({
                    'A': [86, 75, 64, 89, 92],
                    'B': [70, 87, 74, 51, 62],
                    'C': [85, 42, 62, 33, 66]
pandas create dataframe with column names

Finding the columns you want to rename

Now if you have a dataset with so many columns, it’s difficult to see them by printing the data frame. Instead, you can only print all columns like below.

Also Read: How to reset index Pandas



['A', 'B', 'C']


Index(['A', 'B', 'C'], dtype='object')

Renaming a single column

Now let’s say you just want to rename column “A” to “new_column

df1 = df.rename(columns={'A': 'new_column'})
rename specific column in pandas

Renaming multiple columns

If you want to change name of all columns of your dataframe. You can rename those columns with a dictionary where you can use dictionary keys and values to rename columns in a pandas DataFrame.

df2 = df.rename(columns={'A': 'newName1', 'B': 'newName2', 'C': 'newName3'})
rename multiple columns in pandas

Rename columns with list

You can change all column names by assigning a list of new column names.

df.columns = ['list_col1', 'list_col2', 'list_col3']
rename column with list pandas

Rename column by index position

You can rename a specific column by its index position.

df.rename(columns={ df.columns[1]: "new_col_index_1" }, inplace = True)
change column name by index position pandas

Adding a new column

Let’s try to add new column to our pandas dataframe.

import numpy as np

df_len = len(df)
new_val = pd.Series(np.random.randn(df_len))
df['new_col'] = new_val

add new column to dataframe pandas

Removing a column

Now if you want to delete any specific column from a pandas dataframe:

del df['new_col']
rename column by index position pandas


In this pandas rename column tutorial, we learned how to rename a column in pandas. We first renamed the column or header as new_column, then wrote code to rename all the column names with a certain prefix. Next, we used .rename() function in pandas to specify new column names for each row.

Also Read:  Roadmap to Learn Django for Beginners

That’s it for today, if you have any questions or suggestions regarding this tutorial, feel free to mention those in the comment section below.

Leave a comment