How to rename a DataFrame index in Pandas?
source link: https://thispointer.com/how-to-rename-a-dataframe-index-in-pandas/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
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.
# 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.
Pandas Tutorials -Learn Data Analysis with Python
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.
Join a LinkedIn Community of Python Developers
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK