Pandas Set Index Name To DataFrame - Spark By {Examples}

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.

Advertisements

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

Key Points –

  • The index name provides a label for the index, enhancing clarity and usability in DataFrame operations.
  • Use the set_index() method to establish one or more columns as the DataFrame’s index while optionally naming it.
  • You can directly assign a name to the index using the df.index.name attribute.
  • The inplace parameter in set_index() allows modifications to be made directly to the original DataFrame.
  • The drop parameter in set_index() determines whether the original column used for the index should be retained in the DataFrame.
  • Use the reset_index() method to revert to the default integer index and convert the current index back to a column.

Quick Examples of Pandas Set Index Name

Following are quick examples of setting the index name to pandas DataFrame.

# Quick examples of pandas set index name # 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'])

To run some examples of set index names to Pandas DataFrame, let’s create Pandas DataFrame using data from a dictionary.

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

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

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

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

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

Use set_index() to Add Multiple Index

To add multiple indexes using DataFrame.set_index(), you can pass a list of column names to the keys parameter. Below are examples of 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

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

Rename Multiple 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

Complete Examples of 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)

FAQ on Pandas Set Index Name to DataFrame

How do I set the index name in a Pandas DataFrame?

You can set the index name using the index.name attribute. For example: df.index.name = 'my_index'.

Can I set the index name when creating a DataFrame?

You can specify the index name directly when creating a DataFrame by using the index parameter: df = pd.DataFrame(data, index=index_name).

How do I set multiple index names in a MultiIndex DataFrame?

For a MultiIndex, you can set multiple index names by assigning a list to the names attribute: df.index.names = ['level_1', 'level_2'].

Can I set the index name based on an existing column?

You can set the index name after setting the column as the index using the set_index() method. For example: df.set_index('column_name', inplace=True).

How do I reset the index and drop the index name?

You can reset the index and remove the index name by using reset_index(drop=True) or reset_index() without any arguments to retain the index name as a column.

Can I set the index name to a custom string or a variable?

The index name can be set to any string or variable, such as df.index.name = my_variable.

Conclusion

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

  • Pandas Drop Rows by Index
  • Pandas Get Index from DataFrame?
  • Pandas Index Explained with Examples
  • Convert Pandas DatetimeIndex to String
  • Pandas DataFrame reindex() Function
  • Pandas.Index.drop_duplicates() Explained
  • Pandas Set Column as Index in DataFrame
  • Pandas set_index() – Set Index to DataFrame
  • Pandas Get Column Name by Index or Position
  • How to Drop Rows From Pandas DataFrame
  • Change the Order of Pandas DataFrame Columns
  • How to Combine Two Series into pandas DataFrame

Tag » How To Name Index Pandas