Many situations in which you would like to rename the index of a pandas dataframe will occur. For example, if you have a data frame with some numerical index and want an alphabetic index naming. However, DataFrames have a Pandas index, where each column in your DataFrame is listed with its value and location. Moreover, it organizes the data into columns or table formats. However, You can consider the index as a dataframe’s table of contents. In this article, we will demonstrate, using Python 3, how to rename an index in a pandas dataframe.
An index value is bolded, and each index indices represents its row label. However, the Following are the approaches to renaming the index of a Pandas Dataframe in Python:
- Rename the index of your dataframe by using the rename()
- using the index() function
- Using Index property in dataframe
- set_index() and index.names() to rename the index In Python
- Use Inplace() method to rename the index of a Pandas Dataframes in Python
- Using set_axis() to rename the index in Python
- rename_axis() function to rename the index in Python
- Using the index property of rename_axis() function
- Using reindex the dataframe to rename the index of dataframe
*Working on Jupyter Notebook Anaconda Environment.
Using the Built-in rename() function
Using rename() built-in function, you can rename the index into an interpretable index naming. In the following example, the dataframe is formed from the “rand” function by importing a random module from the NumPy library to assign arbitrary values to the datfarame. Furthermore, the split() function divides or differentiates the values of the dataframe after the values have been defined and set.
The execution process executes in the following steps.
- Importing a NumPy and Pandas library
- Now, from the random library, import the “rand” function.
- In the next step, create a random dataframe from the rand function of 2 rows, and 3 columns.
- You can see the original dataframe in the code example below.
- In this python liner code, import the built-in rename() function from the Python library. Now pass the rename index in the rename() function argument and close the argument within curly parenthesis.
- In the output, you can see that the new index name is assigned to the dataframe. The same you can do for the column name, bypassing the column within the arguments of rename() function and renaming the column.
from numpy.random import rand
df=pd.DataFrame(rand(2,3),index='Z Y'.split(),columns='A B C'.split())
df
A B C
Z 0.424650 0.588790 0.685686
Y 0.516275 0.025576 0.045765
index_rename = df.rename(index={'Z': '01', 'Y': '02'})
index_rename
A B C
01 0.424650 0.588790 0.685686
02 0.516275 0.025576 0.045765
Another example with a different dataframe:
df = pd.DataFrame(data={'flower':['Red Ginger','Tree Poppy'],'test':['similarities','accuracy']}, index='0 1'.split(),columns='flower test'.split())
df
flower test
0 Red Ginger similarities
1 Tree Poppy accuracy
index_rename = df.rename(index={'0': 'cluster_0', '1': 'cluster_1'})
index_rename
flower test
cluster_0 Red Ginger similarities
cluster_1 Tree Poppy accuracy
using the index() function
However, renaming the index of a Pandas Dataframe in Python is possible using the index() function. Here the index naming is directly assigned to the index location (rows labels) respectively within square brackets.
df= pd.DataFrame({'data':['dataset','cloud', 'IoT', 'BigData'],
'entry':['A','B','C','D'],
'val':[10,20,30,40]
} )
df
The output of the original Dataframe:
data entry val
0 dataset A 10
1 cloud B 20
2 IoT C 30
3 BigData D 40
df.index = ['entry1', 'entry2', 'entry3','entry4']
df
The output of index rename of Dataframe:
data entry val
entry1 dataset A 10
entry2 cloud B 20
entry3 IoT C 30
entry4 BigData D 40
Using Index property in dataframe
When invoking a dataframe with the Pandas module, renaming the index of a Pandas Dataframe in Python is possible using the index property of the dataframe.
You can check whether the dataframe() possesses the built-in index property by clicking right after the curly braces on the dataframe() function and pressing the Shift+tab keys. It will show all the properties that the respective function takes.
To deal with dataframes, you need to import a library into your Python environment. The syntax follows:
import pandas as pd
Import pandas as pd: What does that mean?
- Adding this functionality or library to my Python code by importing it using the import command.
- To handle the dataframe, you’ll be importing Pandas, the library you want to import into your Python environment.
- It is the Python nomenclature for creating an alias with the As keyword. Using this term, we can refer to long words as short ones in Python.
- Pandas are commonly referred to as pd, a standard alias for Pandas library.
Here is how you can rename the index of the pandas dataframe in python:
- Create a dataframe
- Now in the dataframe, renew or rename the index of a Pandas dataframe in Python using the index property.
- Define index values or new row indices.
- The Dataframe() index property will replace the by-default index values with renewed ones. However, using this approach, you can rename the index of a Pandas in Python.
#Creating a new dataframe
import pandas as pd
df = pd.DataFrame({'Players': ['Fakhar Zaman', 'JN Malan', 'JC Buttler', 'Ibrahim Zadran', 'Babar Azam'],
'Runs': ['193','177*','162*', '162', '158']})
print(df)
Players Runs
0 Fakhar Zaman 193
1 JN Malan 177*
2 JC Buttler 162*
3 Ibrahim Zadran 162
4 Babar Azam 158
Using Dataframe() index property to renew the index in Python.
#rename index(label)
#creating a Dataframe() with Pandas library
df = pd.DataFrame({'Players': ['Fakhar Zaman', 'JN Malan', 'JC Buttler', 'Ibrahim Zadran', 'Babar Azam'],
'Runs': ['193','177*','162*', '162', '158']},
index=['Rank_1','Rank_2','Rank_3', 'Rank_4', 'Rank_5'] )
print(df)
Players Runs
Rank_1 Fakhar Zaman 193
Rank_2 JN Malan 177*
Rank_3 JC Buttler 162*
Rank_4 Ibrahim Zadran 162
Rank_5 Babar Azam 158
set_index() and index.names() to rename the index In Python
The following syntax allows us to access the indices and rename the index in Python.
df = df.set_index(‘label’)
df.index.names = [‘rename label’]
Using set_index() and index.names() in the Python dataframe, you can replace the indices of the columns with the new label. Here in the following Source code, the index label “Runs” is replaced or rename to “scores”.
df = pd.DataFrame({'Players': ['Fakhar Zaman', 'JN Malan', 'JC Buttler', 'Ibrahim Zadran', 'Babar Azam'],
'Runs': ['193','177*','162*', '162', '158']},
index=['Rank_1','Rank_2','Rank_3', 'Rank_4', 'Rank_5'],
columns = ["Players","Runs"])
df = df.set_index('Runs')
df.index.names = ["scores"]
print(df)
Players
scores
193 Fakhar Zaman
177* JN Malan
162* JC Buttler
162 Ibrahim Zadran
158 Babar Azam
Use Inplace() method to rename the index of a Pandas Dataframes in Python
Use rename() function inplace property to rename the index of a dataframe in Python.
The syntax follows:
df.rename(columns={'Players': 'Team Player'}, index={'Rank_1': '1','Rank_2': '2'}, inplace=True)
Where:
Columns, index, and inplace are the property of the rename() function.
To accomplish this task, you need to set the renew index and inplace=True. So that the rename() function replaces the indices of the rows with the set new name of the index.
Here is how it accomplished the task of renaming the index in Python:
df = pd.DataFrame({'Players': ['Fakhar Zaman', 'JN Malan', 'JC Buttler', 'Ibrahim Zadran', 'Babar Azam'],
'Runs': ['193','177*','162*', '162', '158']},
index=['Rank_1','Rank_2','Rank_3', 'Rank_4', 'Rank_5'],
columns = ["Players","Runs"])
df.rename(columns={'Players': 'Team Player'}, index={'Rank_1': '1','Rank_2': '2'}, inplace=True)
print(df)
Team Player Runs
1 Fakhar Zaman 193
2 JN Malan 177*
Rank_3 JC Buttler 162*
Rank_4 Ibrahim Zadran 162
Rank_5 Babar Azam 158
Using set_axis() to rename the index in Python
set_axis() function in Python helps us to rename the axis of a Dataframe. The axis represents the indices of the rows (index) and columns. Using the axis property of a set_axis() you can rename all the indexes in one go.
Note here that set_axis() allows renaming all the index simultaneously in one go, like what we do in inplace property (above section).
Rename the index axis of Pandas Dataframe in Python
df = pd.DataFrame({'Players': ['Fakhar Zaman', 'JN Malan', 'JC Buttler', 'Ibrahim Zadran', 'Babar Azam'],
'Runs': ['193','177*','162*', '162', '158']},
index=['Rank_1','Rank_2','Rank_3', 'Rank_4', 'Rank_5'],
columns = ["Players","Runs"])
#set_axis to rename index (rows)
#set_axis has limitation that it renames all the index values
df=df.set_axis(['01', '02', '03', '04','05'], axis='index')
df
Players Runs
01 Fakhar Zaman 193
02 JN Malan 177*
03 JC Buttler 162*
04 Ibrahim Zadran 162
05 Babar Azam 158
Rename the columns axis of Pandas Dataframe in Python
df = pd.DataFrame({'Players': ['Fakhar Zaman', 'JN Malan', 'JC Buttler', 'Ibrahim Zadran', 'Babar Azam'],
'Runs': ['193','177*','162*', '162', '158']},
index=['Rank_1','Rank_2','Rank_3', 'Rank_4', 'Rank_5'],
columns = ["Players","Runs"])
#set_axis to rename columns
df=df.set_axis(['Players_name', 'Players_score'], axis='columns')
df
Players_name Players_score
Rank_1 Fakhar Zaman 193
Rank_2 JN Malan 177*
Rank_3 JC Buttler 162*
Rank_4 Ibrahim Zadran 162
Rank_5 Babar Azam 158
rename_axis() function to rename index in Python
Using the rename_axis() function, you can label the pandas index in Python. rename_axis() allows us to name the indices of the rows and columns and gives them a logical name.
Here is how you perform the task to renew the index of a Dataframes:
Name the rows axis
df = pd.DataFrame({'Players': ['Fakhar Zaman', 'JN Malan', 'JC Buttler', 'Ibrahim Zadran', 'Babar Azam'],
'Runs': ['193','177*','162*', '162', '158']},
index=['Rank_1','Rank_2','Rank_3', 'Rank_4', 'Rank_5'],
columns = ["Players","Runs"])
df = df.rename_axis("Ranking")
df
Players Runs
Ranking
Rank_1 Fakhar Zaman 193
Rank_2 JN Malan 177*
Rank_3 JC Buttler 162*
Rank_4 Ibrahim Zadran 162
Rank_5 Babar Azam 158
Name the column axis
#creating a dataframe
df = pd.DataFrame({'Players': ['Fakhar Zaman', 'JN Malan', 'JC Buttler', 'Ibrahim Zadran', 'Babar Azam'],
'Runs': ['193','177*','162*', '162', '158']},
index=['Rank_1','Rank_2','Rank_3', 'Rank_4', 'Rank_5'],
columns = ["Players","Runs"])
#rename_axis(index)
df_01 = df.rename_axis("Ranking")
#rename_axis(columns)
df = df_01.rename_axis("WC ranking info", axis="columns")
df
WC ranking info Players Runs
Ranking
Rank_1 Fakhar Zaman 193
Rank_2 JN Malan 177*
Rank_3 JC Buttler 162*
Rank_4 Ibrahim Zadran 162
Rank_5 Babar Azam 158
Using the index property of rename_axis() function
rename_axis() function has an index property that allows us to rename the index axis label. In the following example, the index name “Ranking” is replaced or renamed with ‘Cricket WC Record Ranking’.
#creating a dataframe
df = pd.DataFrame({'Players': ['Fakhar Zaman', 'JN Malan', 'JC Buttler', 'Ibrahim Zadran', 'Babar Azam'],
'Runs': ['193','177*','162*', '162', '158']},
index=['Rank_1','Rank_2','Rank_3', 'Rank_4', 'Rank_5'],
columns = ["Players","Runs"])
#rename_axis(index)
df_01 = df.rename_axis("Ranking")
#rename_axis(columns)
df = df_01.rename_axis("WC ranking info", axis="columns")
#using index property of rename_axis() function
df.rename_axis(index={'Ranking': 'Cricket WC Record Ranking'})
WC ranking info Players Runs
Cricket WC Record Ranking
Rank_1 Fakhar Zaman 193
Rank_2 JN Malan 177*
Rank_3 JC Buttler 162*
Rank_4 Ibrahim Zadran 162
Rank_5 Babar Azam 158
reindex the dataframe
reindex() function allows us to search and match the set index label with the defined index labels. If no record is found while searching, it will return or manipulate the location with NaN for that row-column data.
The execution executes the task in the following steps:
- Define an index for the Pandas Dataframe
- In a dataframe, use the index=index property to accomplish the desired task.
- Now define the new index or updated renamed index.
- And in the final step, compare the entities using the reindex() method.
- It will replace the unknown or mismatched, or missing data with NaN.
index=['Rank_1','Rank_2','Rank_3', 'Rank_4', 'Rank_5']
#creating a dataframe
df = pd.DataFrame({'Players': ['Fakhar Zaman', 'JN Malan', 'JC Buttler', 'Ibrahim Zadran', 'Babar Azam'],
'Runs': ['193','177*','162*', '162', '158']},
#index=['Rank_1','Rank_2','Rank_3', 'Rank_4', 'Rank_5'],
index=index,
columns = ["Players","Runs"])
#reindex()
rename_index = ['Rank_10','Rank_1', 'Rank_2', 'Rank_3']
df=df.reindex(rename_index)
df
Players Runs
Rank_10 NaN NaN
Rank_1 Fakhar Zaman 193
Rank_2 JN Malan 177*
Rank_3 JC Buttler 162*
Summing Up
On this Page, you learned the different approaches to renaming the index of a dataframe in pandas in Python. The method you choose will depend entirely on the needs of your program.
If you want to learn more about Python Programming, visit Python Programming Tutorials.