Is there any way to remove index name in pandas dataframe?

Yes, it is possible to remove the index name from a pandas dataframe using the df.rename_axis() function. This function enables you to rename the index name to None, which effectively removes it from the dataframe. However, this won’t actually delete the index, just the name. To completely remove the index, you can use the df.reset_index() function. This will remove the index and add the data as a new column in the dataframe.


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.

x