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()
Dataframe 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 @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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With