The Python Oracle

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

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Orient Looping

--

Chapters
00:00 Pandas: Setting Column As Index With Set_index() Creates A Sub-Index. Why Is It Happening And How To
00:51 Accepted Answer Score 3
01:11 Thank you

--

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

--

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