如何熔化多索引行并维护Pandas列中的索引信息?

How to melt multindex row and maintain the index information in the column with Pandas?

给定如下多索引行

            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

起草了以下内容

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()

但是,我无法将原始索引维护到新列中。我可以知道在哪里编辑参数吗?

如果需要避免删除原始 index,请在 DataFrame.melt 中使用 ignore_index=False:

df_2=df.melt(ignore_index=False)
print (df_2)
      variable     value
bar a      w_0 -1.123297
    b      w_0  1.796706
    c      w_0 -0.525515
car d      w_0  0.213163
    e      w_0  0.012576
    f      w_0  1.190820
bar a      w_1  0.227907
    b      w_1 -0.714894
    c      w_1  0.206039
car d      w_1  0.434408
    e      w_1  2.269184
    f      w_1 -0.818724
bar a      w_2  1.823298
    b      w_2  0.390618
    c      w_2 -0.413115
car d      w_2  0.957533
    e      w_2  0.929579
    f      w_2 -0.615583
bar a      w_3 -0.573985
    b      w_3  0.068603
    c      w_3  1.329189
car d      w_3  0.312509
    e      w_3 -0.046269
    f      w_3 -0.204653
bar a      w_4  1.047487
    b      w_4 -0.565013
    c      w_4  0.425986
car d      w_4 -2.239224
    e      w_4  0.793489
    f      w_4 -0.200557

在您的预期输出中似乎需要转置:

df_2=df.T
print (df_2)
          bar                           car                    
            a         b         c         d         e         f
w_0 -0.201430  1.114928  1.614460  0.125812 -2.414507  1.108207
w_1  0.870552 -0.126325  1.469310  0.123729  0.547269  1.294423
w_2  0.906490 -0.123776 -1.259072 -0.588008  2.129931 -1.128339
w_3 -1.316442 -0.840914  0.577343  2.459818  1.083407 -1.032046
w_4 -1.954460  1.904081 -0.209807 -0.900796  0.431888 -0.858203