• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:14 mins read
You are currently viewing Pandas Set Index Name to DataFrame

Use pandas.DataFrame.rename_axis() to set the index name/title, in order to get the index use DataFrame.index.name property and the same could be used to set the index name as well. When you create a DataFrame, by default pandas create an index name as 'Index'. By using the approaches mentioned in this article, you can set the custom name to index.

Alternatively, the index name can be set using df.index.rename(), df.index.set_names().

1. Quick Examples of Pandas Set Index Name

In case you hurry, below are some quick examples of how to set the index name to pandas DataFrame.


# Below are quick examples.

# Example 1: Get name of the index column of DataFrame.
index_name=df.index.name

# Example 2: Set Index Name
df.index.name='Index1'

# Example 3: Set column as Index.
df = pd.DataFrame(technologies).set_index('Courses')

# Example 4: Rename Index.
df = df.rename_axis('Courses1')

# Example 5: Get pandas index title/name by index and columns parameter.
df = df.rename_axis(index='Courses', columns="Courses1")

# Example 6: Removing index and columns names to set it none.
df = df.rename_axis(index=None, columns=None)

# Example 7: Using df.index.rename get pandas index title/name.
df.index.rename('Courses1', inplace=True)

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

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

# Example 10: 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 the results. Our DataFrame contains column names CoursesFeeDuration, and Discount.


# Create DataFrame
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("Create DataFrame:\n", df)

Yields below output.

pandas set index name

By default, the DataFrame assigns an Index name as Index and this is not shown on the DataFrame output.

2. Get Index Name From Pandas DataFrame

As I said above, Pandas assign a default name to the Index column, you can get this using DataFrame.index.name


# Get name of the index column of DataFrame.
index_name=df.index.name

Now, let’s set a custom Index name to the DataFrame.


# Set/Change the DataFrame index name
df.index.name = 'Index_Name'
print("Set the index name to DataFrame:\n", df)

Yields below output.

pandas set index name

3. Assign Column as Index using set_index()

Use pandas.DataFrame.set_index() method to set a column as an index. In the below example, I am setting the column Courses as Index. When you do this, the column name is assigned as an index name and it will be removed from columns.


# Set Existing column as Index
df = pd.DataFrame(technologies).set_index('Courses')
print(df)

# Output:
#           Fee Duration  Discount
# Courses                          
# Spark    20000   30days      1000
# PySpark  25000   40days      2300
# Python   22000   35days      1200
# Pandas   30000   50days      2000

# To Get index as columns name.
print (df.index.name)

# Output:
# Courses

4. Pandas set Index Name using rename_axis()

You can also rename the Index name using rename_axis(), just pass the index name you wanted as an argument to this method. This method also takes several arguments like inplace=True, axis=1 and more, refer to pandas documentation for more details.


# Set Index Name using rename_axis() method.
df = df.rename_axis('Courses1')
print(df)


# Output:
#            Fee Duration  Discount
# Courses1                          
# Spark     20000   30days      1000
# PySpark   25000   40days      2300
# Python    22000   35days      1200
# Pandas    30000   50days      2000

It sets the name of the index column of the DataFrame to Courses1. Now let’s see how to rename the axis of the Index column. For example, set the axis name as Courses_Name


# Rename index column by rename_axis() method
df = pd.DataFrame(technologies).set_index('Courses').rename_axis('Courses_Name', axis=1)
print(df)

Yields below output.


# Output:
Courses_Name    Fee Duration  Discount
Courses                               
Spark         20000   30days      1000
PySpark       25000   40days      2300
Python        22000   35days      1200
Pandas        30000   50days      2000

You can also use the parameters index and column in order to get pandas index title/name. For example-


# Get pandas index title/name by index and Column parameter.
df = pd.DataFrame(technologies,index=index_labels)
df = df.rename_axis(index='RowNumber', columns="Row")
print(df)

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

# Removing index and columns names.
df = df.rename_axis(index=None, columns=None)
print(df)

# Output: Index and Columns to set none
.
#           Fee Duration  Discount
# Spark    20000   30days      1000
# PySpark  25000   40days      2300
# Python   22000   35days      1200
# pandas   30000   50days      2000

5. Using DataFrame.index.rename() get Pandas Index Title/Name

In this section, I will use df.index.rename with inplace=True parameter in order to set the index name on the existing DataFrame.


# Using de.index.rename get pandas index title/name.
df.index.rename('Row', inplace=True)
print(df)

Yields below output.


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

6. Add Multiple Index Using DataFrame.set_index()

By using DataFrame.set_index() you can also set multiple existing columns as indexes. Below examples append 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
Row   Courses Duration                 
r1    Spark   30days    20000      1000
r2    PySpark 40days    25000      2300
r3    Python  35days    22000      1200
r4    pandas  50days    30000      2000

7. 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 levels of row indexes. For example-


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

Yields below output.


# Output:

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

8. Rename Multiple/All Levels of Indexes

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


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

Yields below output


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

9. Complete Examples of pandas Set Index Name


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)

# Get name of the index column of DataFrame.
df.index.name
'Index'
df.index.name='Index1'
print(df)

# Get pandas index/name by set_index.
df = pd.DataFrame(technologies).set_index('Courses')
print(df)

# To get Index and Column names.
print (df.index.name)
print (df.columns.name)

# Rename a column by rename_axis method.
df = df.rename_axis('Courses1')
print(df)

# Rename index column by rename_axis() method
df = pd.DataFrame(technologies).set_index('Courses').rename_axis('Courses_Name', axis=1)
print(df)

# Get pandas index title/name by index and Column parameter.
df = df.rename_axis(index='RowNumber', columns="Row")
print(df)

# Removing index and columns names to set it none.
df = df.rename_axis(index=None, columns=None)
print(df)

# Using de.indx.rename get pandas index title/name.
df.index.rename('Row', inplace=True)
print(df)

# 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(['Row','Courses_Name','Courses_Duration'])
print(df2)

10. Conclusion

In this article, you have learned about getting and setting index column names using set_index(), rename_axis() methods, also renaming the index by rename_axis() with several parameters.

Reference

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