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 creates an index name as 'Index'
. By using the approaches mentioned in this article, you can set the custom name to index.
Alternatively, 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.
# 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 columns 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 df.index.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 Courses
, Fee
, Duration
, 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(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
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)
# Output:
# 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)
# Output:
# 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. 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 and columns names.
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 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
# 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 name using set_index(), rename_axis() methods, also rename index by rename_axis() with several aparamters.
Related Articles
- How to Drop Rows From Pandas DataFrame Examples
- How to Merge Series into Pandas DataFrame
- Change the Order of Pandas DataFrame Columns
- How to Combine Two Series into pandas DataFrame
- Pandas Set Value to Particular Cell in DataFrame Using Index
- Pandas Set Column as Index in DataFrame
- Pandas set_index() – Set Index to DataFrame
- Pandas Set Index to Column in DataFrame
- Get Count of Each Row of Pandas DataFrame