扩展 pandas 多重索引,为每个索引添加 N 个新级别?

cho*_*raf 6 python scipy pandas

我经常遇到这样的情况:我有一个 pandas 多重索引,其级别如下:

ix = pd.MultiIndex.from_tuples(((1, 2),
                                (1, 3),
                                (2, 2),
                                (2, 5)), names=['hi', 'there'])
a = pd.DataFrame([0]*4, index=ix, columns=['foo'])
Run Code Online (Sandbox Code Playgroud)

具有这种结构:

print a
          foo
hi there   
1  2      0
   3      0
2  2      0
   5      0
Run Code Online (Sandbox Code Playgroud)

但是,我想扩展这些索引,例如每个级别 3 个新索引。所以我想添加另一个索引,使最终产品看起来像这样:

                  foo
hi there newix     
1  2     1        0
         2        0
   3     1        0
         2        0
2  2     1        0
         2        0
   5     1        0
         2        0
Run Code Online (Sandbox Code Playgroud)

我想不出使用“from_product”之类的明显方法来做到这一点。我想我可以通过迭代前两行来手动构造元组,但这看起来很麻烦。有没有比我想象的更优雅的方法来实现这一点?

编辑:理想情况下,这将不是这样的:

newixs = []
for ix in a.index:
    for i in range(5):
        nix = list(ix) + [i]
        newixs.append(nix)
Run Code Online (Sandbox Code Playgroud)

这可行(使用 from_tuples 来制作 pandas 多重索引),但对我来说似乎很老套:P

And*_*den 1

我首先使用 concat 创建一个更大的 DataFrame:

In [11]: res = pd.concat([a, a])

In [12]: res
Out[12]: 
          foo
hi there     
1  2        0
   3        0
2  2        0
   5        0
1  2        0
   3        0
2  2        0
   5        0
Run Code Online (Sandbox Code Playgroud)

我认为附加新索引的最简单方法是添加一个新列,然后set_index

In [13]: res['newix'] = np.repeat([1, 2], len(a))

In [14]: res
Out[14]: 
          foo  newix
hi there            
1  2        0      1
   3        0      1
2  2        0      1
   5        0      1
1  2        0      2
   3        0      2
2  2        0      2
   5        0      2

In [15]: res.set_index('newix', append=True)
Out[15]: 
                foo
hi there newix     
1  2     1        0
   3     1        0
2  2     1        0
   5     1        0
1  2     2        0
   3     2        0
2  2     2        0
   5     2        0
Run Code Online (Sandbox Code Playgroud)

这基本上就是您想要的(res.sort_index()如果需要,您可以)。