Pandas Rename Index of DataFrame

  • Post author:
  • Post category:Pandas
  • Post last modified:October 5, 2023

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.

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.


# Below are some quick examples

# 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)

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

I am a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, I have honed my expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. My journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. I have started this SparkByExamples.com to share my experiences with the data as I come across. You can learn more about me at LinkedIn

Leave a Reply

You are currently viewing Pandas Rename Index of DataFrame