Pandas Rename Index of DataFrame

  • Post author:
  • Post category:Pandas
  • Post last modified:March 4, 2024
  • Reading time:16 mins read

By using rename_axis(), Index.rename() functions you can rename the row index name/label of a pandas DataFrame. Besides these, there are several ways like df.index.names = ['Index'], rename_axis(), set_index() to rename the index. In this article, I will explain multiple ways of how to rename a single index and multiple indexes of the Pandas DataFrame.

Related: Pandas Rename Column and Pandas Rename or Change Row index Values.

Key Points –

  • Pandas provides the rename_axis() method to rename the index of a DataFrame.
  • This method allows users to change the name of the index without altering the underlying data.
  • Users can specify the new name for the index using the name parameter of the rename_axis() method.
  • Renaming the index can be particularly useful when preparing data for visualization or when exporting data to different formats.
  • The rename_axis() method in Pandas offers a straightforward way to rename the index of a DataFrame without modifying the data itself.
  • Renaming the index can improve the interpretability and clarity of DataFrame operations and analyses.

1. Quick Examples of Rename DataFrame Index

If you are in a hurry, below are some of the quick examples of renaming pandas DataFrame index.


# Quick examples of rename dataframe index

# Add/Change name to Index using df.index.name
df.index.names = ['Index']

# Add/Change name to Index using df.index.rename()
df.index = df.index.rename('Index')
df.index.rename('Index', inplace=True)

# Add/Change name to Index using df.rename_axis()
df2 = df.rename_axis('Index')
df.rename_axis('Index', inplace=True)

# DataFrame.rename_axis() to change axis parameter
df2=df2.rename_axis('Attributes', axis='columns')

# Add Multilevel index using set_index() 
df2 = df.set_index(['Courses', 'Duration'], append=True)

# Rename Single index from multi Level
df2.index = df2.index.set_names('Courses_Duration', level=2)

# Rename All indexes
df2.index=df2.index.rename(['Index','Courses_Name','Courses_Duration'])

Now, let’s create a DataFrame with a few rows and columns, execute these examples and validate results. Our DataFrame contains column names Courses, Fee, Duration, and Discount.


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame(technologies,index=index_labels)
print(df)

Yields below output.


# Output:
    Courses    Fee Duration  Discount
r1    Spark  20000   30days      1000
r2  PySpark  25000   40days      2300
r3   Python  22000   35days      1200
r4   pandas  30000   50days      2000

2. Using rename_axis() to Rename Pandas DataFrame Index

Use DataFrame.rename_axis() to add/rename the column Index. Above DataFrame doesn’t have an Index name and will use this method to add an index label first. Note that this method by default returns a new DataFrame after adding an Index. Use inplace=False to update the existing DataFrame.


# Rename row with index
df1 = df.rename_axis('Index')
print(df1)

Yield the below output.


# Output:
       Courses    Fee Duration  Discount
Index                                   
r1       Spark  20000   30days      1000
r2     PySpark  25000   40days      2300
r3      Python  22000   35days      1200
r4      pandas  30000   50days      2000

Another simple way to add/rename an Index is using DataFrame.index.rename() and df.index.names = ['Index']. These updates the index on the existing DataFrame.


# Using DataFrame.index.rename to change Index
df.index = df.index.rename('Index')
(or)
df.index.names = ['Index']
print(df)

Now, let’s rename the row Index to New_Index from Index label.


# Rename Index
df2 = df.rename_axis('New_Index')

Also, add a column name to Index.


df2=df2.rename_axis('Attributes', axis='columns')
print(df2)

Yields below output.


# Output:
Attributes  Courses    Fee Duration  Discount
New_Index                                    
r1            Spark  20000   30days      1000
r2          PySpark  25000   40days      2300
r3           Python  22000   35days      1200
r4           pandas  30000   50days      2000

4. Add Multiple Index using DataFrame.set_index()

Use DataFrame.set_index() to set the one or multiple existing columns as indexes. Below examples appends columns Courses and Duration to row Index.


# Add Multilevel index using set_index() 
df2 = df.set_index(['Courses', 'Duration'], append=True)
print(df2)

Yields below output.


# Output:
                          Fee  Discount
Index Courses Duration                 
r1    Spark   30days    20000      1000
r2    PySpark 40days    25000      2300
r3    Python  35days    22000      1200
r4    pandas  50days    30000      2000

5. Rename Multi Level Index Names using index.set_names()

By using DataFrame.index.set_names() you can change the index of a specific level when you have multiple level of row indexes.


# Rename Single index from multi Level
df2.index = df2.index.set_names('Courses_Duration', level=2)
print(df2)

Yields below output.


# Output:
                                  Fee  Discount
Index Courses Courses_Duration                 
r1    Spark   30days            20000      1000
r2    PySpark 40days            25000      2300
r3    Python  35days            22000      1200
r4    pandas  50days            30000      2000

6. Rename Multiple/All Level of Indexes

When you have multiple rows indices and if you wanted to rename multiple indices at the same time, use DataFrame.index.rename(). Note that you need to specify all indices as a param.


# Rename All indexes
df2.index=df2.index.rename(['Index','Courses_Name','Courses_Duration'])
print(df2)

Yields below output


# Output:
                                       Fee  Discount
Index Courses_Name Courses_Duration                 
r1    Spark        30days            20000      1000
r2    PySpark      40days            25000      2300
r3    Python       35days            22000      1200
r4    pandas       50days            30000      2000

13. Complete Example For Rename DataFrame Index


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame(technologies,index=index_labels)
print(df)

# Add/Change name to Index using df.index.name
df.index.names = ['Index']
print(df)

# Add/Change name to Index using df.rename_axis()
df1 = df.rename_axis('Index')
print(df1)

# Add/Change name to Index using df.index.rename()
df.index = df.index.rename('Index')
print(df)

# DataFrame.rename_axis() to change axis parameter
df2 = df.rename_axis('New_Index')
df2=df2.rename_axis('Attributes', axis='columns')
print(df2)

# Add Multilevel index using set_index() 
df2 = df.set_index(['Courses', 'Duration'], append=True)
print(df2)

# Rename Single index from multi Level
df2.index = df2.index.set_names('Courses_Duration', level=2)
print(df2)

# Rename All indexes
df2.index=df2.index.rename(['Index','Courses_Name','Courses_Duration'])
print(df2)

Frequently Asked Questions on Rename Index of DataFrame

Why should I rename the index of a DataFrame?

Renaming the index of a DataFrame can enhance readability and improve clarity in data analysis. It allows you to provide more descriptive labels to the index, making it easier to understand the data.

How can I rename the index of a DataFrame in Pandas?

You can rename the index of a DataFrame using the rename_axis() method in Pandas. This method allows you to specify the new name for the index.

Does renaming the index modify the underlying data in the DataFrame?

Renaming the index of a DataFrame using the rename_axis() method does not modify the underlying data in the DataFrame. It only changes the name of the index. The rename_axis() method operates on the labels of the index, not on the data itself. Therefore, it preserves the integrity and contents of the DataFrame while allowing you to provide a more descriptive name for the index.

Can I rename the index to multiple levels in a DataFrame?

You can rename the index to multiple levels, also known as a multi-index or hierarchical index, using the rename_axis() method. This allows for more complex indexing and slicing operations on the DataFrame.

Is it possible to rename the index inplace?

You can rename the index inplace by setting the inplace parameter to True when using the rename_axis() method. This will modify the DataFrame in place without the need to reassign it to a new variable.

Conclusion

In this article, you have learned how to rename pandas DataFrame index using set_axis(), set_index(), index.set_names() and more methods with examples. You have also learned how to rename single and multilevel Index names.

Happy Learning !!

References

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium

Leave a Reply