Pandas中的MultiIndex从数据框转换

3

How would I take a df like this:

Dates          Type           1      2      3                                                                                                                            ...
2018-01-01     Type1        Golf    Van    Jeep
2018-01-02     Type1        Golf    Van    Jeep
2018-01-01     Type2        Golf1   Van1   Jeep1
2018-01-02     Type2        Golf2   Van2   Jeep2

并将其转化为:

                               Type1                    Type2
Dates                    1      2      3           1      2      3                                                                                                               ...
2018-01-01            Golf    Van    Jeep      Golf1    Van1    Jeep1
2018-01-02            Golf    Van    Jeep      Golf2    Van2    Jeep2

编辑: 我想要引入第二个类似于这样的索引:

Type                          Type1                    Type2
Numbers                  1      2      3           1      2      3    
Dates                                                                                                                              ...
2018-01-01            Golf    Van    Jeep      Golf1    Van1    Jeep1
2018-01-02            Golf    Van    Jeep      Golf2    Van2    Jeep2

编辑: 现在,如果我想重新标记所有数字索引值 - 我该如何创建它:

Type                          Type1                    Type2
Numbers                  p1     p2     p3         p1      p2      p3    
Dates                                                                                                                              ...
2018-01-01            Golf    Van    Jeep      Golf1    Van1    Jeep1
2018-01-02            Golf    Van    Jeep      Golf2    Van2    Jeep2

编辑: 可以直接使用: .add_prefix('hh')

2个回答

3
使用DataFrame.set_indexDataFrame.unstack,然后通过DataFrame.swaplevel改变层级顺序并通过DataFrame.sort_indexMultiIndex进行排序。
df = df.set_index(['Dates','Type']).unstack().swaplevel(0,1, axis=1).sort_index(axis=1)
print (df)
Type       Type1             Type2             
               1    2     3      1     2      3
Dates                                          
2018-01-01  Golf  Van  Jeep  Golf1  Van1  Jeep1
2018-01-02  Golf  Van  Jeep  Golf2  Van2  Jeep2

编辑:通过元组添加DataFrame.rename_axis

df = (df.set_index(['Dates','Type'])
        .unstack()
        .swaplevel(0,1, axis=1)
        .sort_index(axis=1)
        .rename_axis(('Type','Numbers'), axis=1))
print (df)
Type       Type1             Type2             
Numbers        1    2     3      1     2      3
Dates                                          
2018-01-01  Golf  Van  Jeep  Golf1  Van1  Jeep1
2018-01-02  Golf  Van  Jeep  Golf2  Van2  Jeep2

太好了,谢谢。我们如何在“Type”下面添加一个级别来命名数字? - Bob
@Bob - 你能在编辑后的问题中展示期望的输出吗? - jezrael
1
非常感谢你的帮助 :) - 如果我想把所有数字都重新标记为p1,p2,p3...,应该怎么做? - Bob
1
啊,我只需要使用.add_prefix('pp') - Bob

1
我理解为先使用 melt 然后再使用 pivot 将其转换回来。
s=df.melt(['Dates','Type']).pivot_table(index=['Dates'],columns=['Type','variable'],values=['value'],aggfunc='sum')
s.columns=s.columns.droplevel(level=0)
s
Out[189]: 
Type       Type1             Type2             
variable       1    2     3      1     2      3
Dates                                          
2018-01-01  Golf  Van  Jeep  Golf1  Van1  Jeep1
2018-01-02  Golf  Van  Jeep  Golf2  Van2  Jeep2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接