Pandas Dataframe Rename Index
To rename the index of a Pandas DataFrame, rename() method is most easier way to rename specific index values in a pandas dataFrame; allows to selectively change index names without affecting other values.
import pandas as pd
data = {'Name': ['John', 'Alice', 'Bob', 'Eve'],
'Age': [25, 30, 22, 35],
'Gender': ['Male', 'Female', 'Male', 'Female'],
'Salary': [50000, 55000, 40000, 70000]}
df = pd.DataFrame(data)
df.rename(index={0: 'Row1', 1: 'Row2'}, inplace=True)
print(df)
Output
Name Age Gender Salary Row1 John 25 Male 50000 Row2 Alice 30 Female 55000 2 Bob 22 Male 40000 3 Eve 35 Female 70000
Let’s explore various methods for renaming the index of a Pandas DataFrame, focusing on the rename_axis() method, direct assignment, the set_index() method, and multi-level indexing. For Other methods implementation we use the same dataset that we have used in above example.
1. Using rename axis
rename_axis() only changes the label of the axis and does not affect the data.
# Rename the index axis to 'Employee ID'
df_renamed_index = df.rename_axis('Employee ID')
print(df_renamed_index)
Output
Name Age Gender Salary Employee ID 0 John 25 Male 50000 1 Alice 30 Female 55000 2 Bob 22 Male 40000 ...
2. Renaming the Index Values Using set index
The set_index() method allows you to set one or more columns as the new index for the DataFrame.Use the set_index() method to set a column (e.g., "Name") as the new index.
# Set 'Name' as the new index
df_with_name_index = df.set_index('Name')
print(df_with_name_index)
Output
Age Gender Salary Name John 25 Male 50000 Alice 30 Female 55000 Bob 22 Male 40000 Eve 35 Female 70000
3. Renaming the Index In-Place Using index.name
If you want to directly rename the index of a DataFrame in place (without creating a new DataFrame), you can modify the name attribute of the index object.
# Rename the index in place
df.index.name = 'Employee ID'
print(df)
Output
Name Age Gender Salary Employee ID 0 John 25 Male 50000 1 Alice 30 Female 55000 2 Bob 22 Male 40000 ...
4. Resetting the Index Using reset index
If you’ve set a custom index using set_index() and want to revert back to the default integer index, you can use the reset_index() method. This method restores the default integer index and optionally keeps the previous index as a column.
# Set 'Name' as index and then reset it
df_with_name_index = df.set_index('Name')
df_reset_index = df_with_name_index.reset_index()
print(df_reset_index)
Output
Name Age Gender Salary 0 John 25 Male 50000 1 Alice 30 Female 55000 2 Bob 22 Male 40000 3 Eve 35 Female 70000
5. Setting Multi-level Index
Pandas allows for more advanced indexing, such as multi-level (hierarchical) indexing, where you can use multiple columns as the index. This is particularly useful for handling complex datasets.
# Set 'Gender' and 'Age' as multi-level index
df_multi_index = df.set_index(['Gender', 'Age'])
print(df_multi_index)
Output
Name Salary Gender Age Male 25 John 50000 Female 30 Alice 55000 Male 22 Bob 40000 Female 35 Eve 70000
Here are some key takeaways:
- Use rename() to change specific index values and set_index() can set a column as the new index in dataframe.
- reset_index() reverts to the default integer index.
- we use rename_axis() to that renames the axis labels