[英]Pandas column multiindex into row multiindex
I have a pandas dataframe我有一个熊猫数据框
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
I cannot figure out how to turn first level of column multiindex into row multiindex to get something like following我无法弄清楚如何将第一级列多索引转换为行多索引以获得如下所示的内容
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
any hint appreciated.任何提示表示赞赏。 Thank you谢谢
干得好:
df.stack(level=0)
As Quang Hoang suggested, I would check out pandas.DataFrame.stack .正如Quang Hoang建议的那样,我会查看pandas.DataFrame.stack 。 The solution should be:解决办法应该是:
df.stack(level=0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.