I have a very wide dataframe where the first column is an index and all subsequent column names I want to become indices too in the melted output. The dataframes have lots of columns so I need a recursive way to do the below on N columns. (As a newbie, this is over my head).
My dataframe is a beginner version of this. I am assuming it is Melt with some complex scope logic, but I am drowning in my confusion right now.
import pandas as pd
df = pd.DataFrame({"GIS": [1, 2, 3], "X0": [100, 200, 300], "X1": [50, 51, 52], "X2": [71, 72, 73], "Xn": [100, 150, 210]})
df = df.set_index(["GIS"])
It produces a df that looks like this:

I don't know how to get there programmatically, especially on really wide dataframes, but ultimately I want it to produce a df that looks like this:
new_df = pd.DataFrame({"GIS": [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3], "Variable": ["X0", "X1", "X2", "Xn", "X0", "X1", "X2", "Xn", "X0", "X1", "X2", "Xn"], "Value": [100, 50, 71, 100, 200, 51, 72, 150, 300, 52, 73, 200]})
new_df = new_df.set_index(["GIS", "Variable"])
Which should produce a df structured like this.

Your suggestions for a bigger are greatly appreciated.
Use
DataFrame.rename_axis,DataFrame.stackandSeries.to_frame:Or
DataFrame.meltwithDataFrame.sort_indexandDataFrame.set_indexwithappend=TrueforMultiIndex: