How to rename the columns of a DataFrame

1 min

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.