Given a multiindex row as below
w_0 w_1 w_2 w_3 w_4
bar a 1.228191 0.912254 1.976780 -0.441382 1.100406
b -0.149423 -0.559502 0.864034 -0.925317 1.366656
c -1.554960 -1.010015 1.313886 0.183036 1.107188
car d -0.603019 -0.596885 -1.252903 -0.256334 0.102575
e 0.808010 -0.780387 0.428621 -0.212347 -1.301377
f -1.604741 0.270343 0.822201 -0.108272 -0.271529
The following was drafted
import numpy as np
import pandas as pd
arrays = [np.array(["bar", "bar", "bar", "car", "car", "car"]),
np.array(["a", "b", "c", "d", "e", "f"]) ]
df = pd.DataFrame(np.random.randn(6, 5), index=arrays)
df.columns = list ( [f'w_{i}' for i in range(0,5)] )
df_2=df.melt()
However, I fail to maintain the original index into the new column. May I know where to edit the parameter.