In this article, we will discuss multiple ways to rename a DataFrame index in pandas.
Table of Contents
Preparing DataSet
To quickly get started, let’s create a sample dataframe to experiment. We’ll use the pandas library with some random data.
import pandas as pd # List of Tuples employees= [('Shubham', 'India', 'Tech', 5, 4), ('Riti', 'India', 'Design' , 7, 7), ('Shanky', 'India', 'PMO' , 2, 2), ('Shreya', 'India', 'Design' , 2, 0), ('Aadi', 'US', 'PMO', 11, 5), ('Sim', 'US', 'Tech', 4, 4)] # Create a DataFrame object from list of tuples df = pd.DataFrame(employees, columns=['Name', 'Location', 'Team', 'Experience', 'RelevantExperience'], index = ['A', 'B', 'C', 'D', 'E', 'F']) print(df)
Contents of the created dataframe are,
Name Location Team Experience RelevantExperience A Shubham India Tech 5 4 B Riti India Design 7 7 C Shanky India PMO 2 2 D Shreya India Design 2 0 E Aadi US PMO 11 5 F Sim US Tech 4 4
Rename specific index(s) in Pandas DataFrame
In this section, we will look at renaming a specific index using the DataFrame.rename()
function. Let’s understand with an example, say, we need to rename the index “A” as “New_A”.
# rename index "A" to "New_A" print(df.rename(index = {'A':'New_A'}))
Output
Name Location Team Experience RelevantExperience New_A Shubham India Tech 5 4 B Riti India Design 7 7 C Shanky India PMO 2 2 D Shreya India Design 2 0 E Aadi US PMO 11 5 F Sim US Tech 4 4
As observed, the first row index which was earlier named as “A” has now changed to “New_A”. In case, we need to rename multiple indexes at once, we can do it as follows.
Frequently Asked:
# rename multiple indexes print(df.rename(index = {'A':'New_A', 'B':'New_B'}))
Output
Name Location Team Experience RelevantExperience New_A Shubham India Tech 5 4 New_B Riti India Design 7 7 C Shanky India PMO 2 2 D Shreya India Design 2 0 E Aadi US PMO 11 5 F Sim US Tech 4 4
Rename all indexes in Pandas DataFrame
In this section, we will discuss the method to rename all the indexes at once in pandas DataFrame. We are going to use the set_axis()
function from pandas, where we need to provide the list of new names and axis (index or columns) value. Let’s rename all the indexes in the above DataFrame.
# rename all indexes print (df.set_axis(["New_A", "New_B", "New_C", "New_D", "New_E", "New_F"], axis='index'))
Output
Name Location Team Experience RelevantExperience New_A Shubham India Tech 5 4 New_B Riti India Design 7 7 New_C Shanky India PMO 2 2 New_D Shreya India Design 2 0 New_E Aadi US PMO 11 5 New_F Sim US Tech 4 4
As observed, all the indexes in the DataFrame are now renamed.
Adding a prefix or suffix to Index Names in Pandas DataFrame
In case, we just need to add a prefix or suffix in the index name of the DataFrame, we can again use the rename function. Let’s add the prefix “New_” in the above DataFrame.
# rename by adding prefix print (df.rename(index = lambda x: "New_"+x))
Output
Name Location Team Experience RelevantExperience New_A Shubham India Tech 5 4 New_B Riti India Design 7 7 New_C Shanky India PMO 2 2 New_D Shreya India Design 2 0 New_E Aadi US PMO 11 5 New_F Sim US Tech 4 4
As observed, we have got a similar output as the above method.
Summary
In this article, we have discussed how to rename DataFrame index in Pandas. Thanks.