python - 来自 Dataframe 的多索引 Pandas

标签 python python-3.x pandas

我将如何采取这样的 df:

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')

最佳答案

使用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

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

相关文章:

python - 如何减少我的 python 应用程序连接到 mysql 服务器的超时

python - LearnPython.org 生成器问题

python - 无法使用 pip 安装 coinbase API

python - pandas:在(多索引)DataFrame上使用每个组中最常见的值执行 fillna() 的最佳方法是什么?

python - 使用 pandas 和 numpy 参数化堆栈溢出的用户数和声誉

python - 小狗。如何动态更改顶点的图片(动画)。 OpenGL

python - 填补 numpy 数组中的空白

python - 如何在 Ubuntu 16+ 上使用 Apache 2.4 设置 Django 1.11 和 Python 3.6.1

python - Scrapy 多个搜索词

python-3.x - Pandas 将 groupby 之后的值计数扩展为列