23.1 C
London
Monday, July 21, 2025
HomePandas in PythonDataFrame Functions in PythonHow to Remove Index Name in Pandas (With Example)

How to Remove Index Name in Pandas (With Example)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following syntax to remove the index name from a pandas DataFrame:

df.index.name = None

This will remove the name from the index column of the DataFrame and leave all other column names unchanged.

The following example shows how to use this syntax in practice.

Example: Remove Index Name in Pandas

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                   'position': ['G', 'G', 'F', 'F', 'G', 'G', 'F', 'F'],
                   'points': [11, 8, 10, 6, 6, 5, 9, 12]})

#specify index name
df.index.names = ['my_index']

#view DataFrame
print(df)

         team position  points
my_index                      
0           A        G      11
1           A        G       8
2           A        F      10
3           A        F       6
4           B        G       6
5           B        G       5
6           B        F       9
7           B        F      12

Currently the index column has the name my_index.

However, we can use the following syntax to remove this index name:

#remove index name
df.index.name = None

#view updated DataFrame
print(df)

  team position  points
0    A        G      11
1    A        G       8
2    A        F      10
3    A        F       6
4    B        G       6
5    B        G       5
6    B        F       9
7    B        F      12

Notice that the name my_index has been removed from the index column while all other column names have remained unchanged.

We can also confirm that the index column no longer has a name by attempting to print the name:

#view index column name
print(df.index.name)

None

We can see that the index column indeed has no name.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

Pandas: How to Get Unique Values from Index Column
Pandas: How to Reset Index After Using dropna()
Pandas: How to Use First Column as Index

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories