Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: setting column as index with set_index() creates a sub-index. Why is it happening and how to remove it?

I want to use the first column in a pandas dataframe as the row index, so I am trying to do it with pandas.set_index(0, inplace=True) but this has the side-effect of adding an extra column index.

(jupyter notebook code running with option InteractiveShell.ast_node_interactivity = "all"):

import pandas as pd

df = pd.DataFrame([[l+r*10 for l in range(1, 5)] for r in range(1, 5)])

df # before

df.set_index(0, inplace=True)

df # after

Dataframe before set_index()

before set_index()

Dataframe after set_index()

after set_index()

The question is why is there a second index created and how to remove it? The set_index() docs don't mention anything about a sub-index being created.

like image 591
ccpizza Avatar asked Sep 13 '25 00:09

ccpizza


1 Answers

Like @scott-boston commented, the index name is shown, which was the column name "0" in your example. If you like to remove it, you can simply use:

del df.index.name

So your full code would be:

import pandas as pd

df = pd.DataFrame([[l+r*10 for l in range(1, 5)] for r in range(1, 5)])

df # before

df.set_index(0, inplace=True)
del df.index.name

df # after
like image 190
PythonSherpa Avatar answered Sep 14 '25 14:09

PythonSherpa