【发布时间】:2021-12-17 23:34:57
【问题描述】:
我有一个熊猫数据框
df = pd.DataFrame([[i+10*j for i in range(6)] for j in range(5)], index=[f"item{i}" for i in range(5)])
df.columns = pd.MultiIndex.from_product((["abc", "xyz"], ["one", "two", "three"]))
abc xyz
one two three one two three
item0 0 1 2 3 4 5
item1 10 11 12 13 14 15
item2 20 21 22 23 24 25
item3 30 31 32 33 34 35
item4 40 41 42 43 44 45
我无法弄清楚如何将第一级列多索引转换为行多索引以获得类似以下的内容
one two three
item0 abc 0 1 2
xyz 3 4 5
item1 abc 10 11 12
xyz 13 14 15
item2 abc 20 21 22
xyz 23 24 25
item3 abc 30 31 32
xyz 33 34 35
item4 abc 40 41 42
xyz 43 44 45
任何提示表示赞赏。 谢谢
【问题讨论】:
-
df.stack(level=0)