gpt4 book ai didi

python - Pandas 列多索引到行多索引

转载 作者:行者123 更新时间:2023-12-02 01:54:08 25 4
gpt4 key购买 nike

我有一个 Pandas 数据框

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)

关于python - Pandas 列多索引到行多索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69827390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com