import pandas as pd
df = pd.DataFrame(data={'start':[1,2,3],'zone':['a','b','c']});
df['end']=[4,5,6]
df.set_index('zone',drop=True,inplace=True,append=False)
print(df)

      start  end
zone
a         1    4
b         2    5
c         3    6

我想将此数据框修改为:
zone  a              b            c
      start  end     start  end   start  end
      1        4     2      5     3      6

通过将行索引更改为列索引。我需要这个,因为我想融化数据帧稍后,所以它将成为一个长表但我不知道如何将行索引和列索引融为一体但是得到这个输出将允许我简单地使用:
pd.melt(df)

去拿那张长桌子。
如果有办法将列索引更改为行索引,那么我可以跳过将行索引更改为列索引的步骤,直接获取长表。
请帮忙。

最佳答案

尝试使用stackto_frame和t进行转置:

df.stack().to_frame().T

输出:
zone     a         b         c
     start end start end start end
0        1   4     2   5     3   6

10-07 19:12
查看更多