1 Upvote

Renaming DataFrame Columns Using Dictionaries & Lists

Python
Data Preprocessing

Here we have two examples of how to rename columns in a Pandas DataFrame.

In the 1st example we rename a single column by using the rename function and passing a dictionary as a parameter containing the current column name as the key and new column name as the value.

In the 2nd example we simply use a list to set the names of all columns in the dataframe.

#Rename Columns with a Dictionary using rename
df.rename(columns={'Order_Date' :'Date'},inplace=True)

#Rename all columns with a list
df.columns = ['col_1','col_2','col_3']

By analyseup - Last Updated Jan. 12, 2022, 12:46 a.m.

Did you find this snippet useful?

Sign up to bookmark this in your snippet library

COMMENTS
RELATED SNIPPETS
Pivoting Pandas Dataframes
Python
Data Preprocessing

Pandas

3
2
Top Contributors
104
100