Pandas Set and Get Index Title/Name of DataFrame

This article explains how to set and get pandas DataFrame index title/name by using name attribute and rename_axis(), set_index() method.

1. Quick Examples of Index Title/Name of Pandas

In case if you hurry, below are some quick examples.


# Below are quick example
# Get name of the index column of DataFrame.
index_name=df.index.name

# set Index Name
df.index.name='Index1'

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

# Rename Index.
df = df.rename_axis('Courses1')

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

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

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

# 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 CoursesFeeDuration, 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.


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

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
print(index_name)

# Outputs
# Index

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


# Set/Change the DataFrame index name
df.index.name = 'Index_Name'
print(df)

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

3. Set Existing Column as Index using set_index()

Use set_index() method to assign an existing column of a DataFrame as a row 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 and columns names.
print (df.index.name)

# Ouputs
Courses

4. 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.


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)

# 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 of 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.


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

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.


                          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.



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

Yields below 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 Index Title/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)

Conclusion

In this article, you have learned about getting and setting index column name using set_index(), rename_axis() methods, also rename index by rename_axis() with several aparamters.

You May Also Like

Reference

Leave a Reply

Pandas Set and Get Index Title/Name of DataFrame