Follow the steps below to change the names of one or multiple columns by their index number in your Pandas DataFrame.
Step # 1: Acquire your DataFrame
For this example we will start by importing the pandas library and create a DataFrame with several columns:
import pandas as pd
cols_lst = ['Month', 'Month Name', 'Sales', 'Expenses']
sales_df = pd.DataFrame(columns = cols_lst )
Note: Make sure to import the pandas library before calling DataFrame methods.
Step # 2: Identify your column position
We can now easily look into the column index:
sales_df.columns
This will return:
Index(['Month', 'Month Name', 'Sales', 'Expenses'], dtype='object')
Let’s check the number of columns of our DataFrame:
len (sales_df.columns)
This will return: 4
We can use the brackets notation to refer to each of the elements of the index. So for example:
sales_df.columns[1] # will return the value Month Name
sales_df.columns[3] # will return the value Expenses
Note: Remember that the index position count starts at 0. If we call the element in position 4 we will get an Index Error:
sales_df.columns[4] # will return an exception
Will return:
IndexError: index 4 is out of bounds for axis 0 with size 4
Step # 3: Rename your column by index and position
Now that we understand the ins and outs of the column index we can proceed with renaming specific columns as needed. We will use the DataFrame.rename method and pass a mapping dictionary with the new column name values.
Rename one column
By using a mapper:
sales_df.rename (columns = {sales_df.columns[1]: 'Month_Name', inplace=True)
Noe the usage of the inplace=True parameter to persist the new names in your DataFrame.
Or, by assigning a value directly to the index at a specific position.
sales_df.columns[1] = 'Month_Name]'
Let’s print the column index to see the change:
Index(['Month', 'Month_Name', 'Sales', 'Expenses'], dtype='object')
Rename multiple columns
In a similar fashion we can change multiple column names by position:
sales_df.rename (columns = {sales_df.columns[1]: 'Month_Name', sales_df.columns[3]: 'Operational_Expenses'}, inplace=True)
print(sales_df.columns)
This will return:
Index(['Month', 'Month_Name', 'Sales', 'Operational_Expenses'], dtype='object')
Troubleshooting
A common error you might encounter is that the new columns names are not persisted. Here’s the solution.