The Python Oracle

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

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Horror Game Menu Looping

--

Chapters
00:00 Question
01:15 Accepted answer (Score 2)
01:45 Thank you

--

Full question
https://stackoverflow.com/questions/5186...

Question links:
[pandas.set_index(0, inplace=True)]: https://pandas.pydata.org/pandas-docs/st...
[image]: https://i.stack.imgur.com/MdCTF.png
[image]: https://i.stack.imgur.com/3FtnW.png
[set_index() docs]: https://pandas.pydata.org/pandas-docs/st...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas #dataframe #indexing

#avk47



ACCEPTED ANSWER

Score 3


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