Pandas.Index.Rename
The pandas.Index.rename() is used to alter the index. Using this function, we are able to set the new name. It takes only two parameters.
Syntax:
- The “name” specifies the index label.
- If “inplace” is set to “True” (by default = False), it renames the index of the existing object directly.
Example 1: Rename an Index Label for the DataFrame Object
Create the “detail_cases” DataFrame with four columns that hold three records. The index label is “Case Records”. We use the Index.rename() to rename this index label to “Case ID”.
# Create a DataFrame with 4 columns that holds 3 records
detail_cases = pandas.DataFrame({'Case_Related': ['Computer','Mechanical','Electrical'],
'Source': ['Phone','Email','Web'],
'Priority':['Low','Medium','Medium'],
'Resolved':['Yes','Yes','No']},index=['Case_record_1','Case_record_2','Case_record_3'])
# Set the Index Label as 'Case Records'
detail_cases.index.names = ['Case Records']
print(detail_cases,"\n")
# Using index.rename()
detail_cases.index.rename('Case ID',inplace=True)
print(detail_cases)
Output:
In the previous code, the “inplace” parameter is set to “True”. If we display the DataFrame after changing the index label, the index label is updated to “Case ID”.
Example 2: Rename an Index Label for the Index Object
Create an index with three strings with the label as “Source”. Change it to “Case Source” using the pandas.Index.rename() function.
print(index)
# Display the Index by updating the Index Label to 'Case Source'
print(index.rename('Case Source'))
Output:
Pandas.Index.Names
The pandas.Index.names property is used to return the index label in a FrozenList. We can assign the new index label to this property. This way, we can rename the index.
Syntax:
Example:
Utilize the DataFrame that is created in the first scenario. The index label is “Case Records”. We use the pandas.Index.names property to rename this index label to “Cases”.
# Create DataFrame with 4 columns that holds 3 records
detail_cases = pandas.DataFrame({'Case_Related': ['Computer','Mechanical','Electrical'],
'Source': ['Phone','Email','Web'],
'Priority':['Low','Medium','Medium'],
'Resolved':['Yes','Yes','No']},index=['Case_record_1','Case_record_2','Case_record_3'])
# Set the Index Label as 'Case Records'
detail_cases.index.names = ['Case Records']
print(detail_cases.index.names,"\n")
print(detail_cases,"\n")
# Using index.names
Updated = detail_cases.index.names = ['Cases']
print(detail_cases.index.names,"\n")
print(detail_cases,"\n")
Output:
We display the existing and updated index labels using this property and also display the DataFrames in both scenarios.
Pandas.DataFrame.Rename_Axis
The pandas.DataFrame function is used to set the name of the axis for the DataFrame index or columns. Look at the syntax and the parameters.
Syntax:
- We can specify the label to the “mapper” parameter so that the index label is updated with the specified label.
- If the “axis” is 0 (by default), the rows are renamed and the columns are renamed if it is 1.
- If “inplace” is set to “True” (by default = False), it renames the index of the existing DataFrame directly.
Example:
Utilize the DataFrame that is created in the first scenario. The index label is “Case Records”. We use the pandas.DataFrame.rename_axis function to rename this index label to “Case_Data”.
# Create DataFrame with 4 columns that holds 3 records
detail_cases = pandas.DataFrame({'Case_Related': ['Computer','Mechanical','Electrical'],
'Source': ['Phone','Email','Web'],
'Priority':['Low','Medium','Medium'],
'Resolved':['Yes','Yes','No']},index=['Case_record_1','Case_record_2','Case_record_3'])
# Set the Index Label as 'Case Records'
detail_cases.index.names = ['Case Records']
print(detail_cases,"\n")
# Using rename_axis()
Updated = detail_cases.rename_axis('Case_Data')
print(Updated)
Output:
Rename the Multi-Index
Create the “detail_cases” DataFrame with four columns and with “Priority”, “Resolved”, and “Source” as indices. Change the index labels to “Case Priority”, “Case Resolved”, and “Case Source” using the pandas.Index.names property. We just need to pass the new index labels through the list to this property.
# Create DataFrame with 4 columns that holds 3 records
detail_cases = pandas.DataFrame({'Case_Related': ['Computer','Mechanical','Electrical'],
'Source': ['Phone','Email','Web'],
'Priority':['Low','Medium','Medium'],
'Resolved':['Yes','Yes','No']}).set_index(['Priority', 'Resolved','Source'])
print(detail_cases,"\n")
# Change the Index Labels to 'Case Priority','Case Resolved', 'Case Source'
detail_cases.index.names = ['Case Priority','Case Resolved','Case Source']
print(detail_cases,"\n")
Output:
Currently, the “Priority”, “Resolved”, and “Source” are the indices to the previous DataFrame. Now, the index labels are changed to “Case Priority”, “Case Resolved”, and “Case Source”.
Conclusion
Now, we are able to rename the index label of the Pandas DataFrame. In this guide, we discussed four scenarios that rename the index label of the DataFrame. The pandas.Index.rename() is used to alter the index. We can rename the index by assigning the new index label to the pandas.Index.name property. The pandas.DataFrame function is used to set the name of the axis for DataFrame index or columns.