Home » How to Remove Index Name in Pandas (With Example)

How to Remove Index Name in Pandas (With Example)

by Erma Khan

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

Related Posts