Learn all the possibilities of how to rename columns in Pandas and choose the most convenient one for you.
Pandas rename
The easiest way to rename specific columns is to use Pandas coulmns function. Use the following code:
df.rename(columns={'old_column1_name': 'new_column1_name', 'old_column2_name': 'new_column2_name'}, inplace=True)
Remember to use the inplace parameter to make the column name change permanent.
Documentation for Pandas' rename function is available at: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.rename.html
Pandas columns
You can use the Pandas columns function to change the names of columns in your data frame.
df.columns = ['new_column1_name', 'new_column2_name', 'new_column3_name', 'new_column4_name']
Pandas columns str replace
The name of a column is just a string, so it is also possible to make columns change by changing the names of these strings by using the columns str replace method.
df.columns = df.columns.str.replace('old_name', 'new_name')
1 thought on “How to rename columns”