使用MultiIndex列将pandas列分为两部分

时间:2019-04-24 14:43:23

标签: python-3.x pandas dataframe

我需要将DataFrame列分成两部分,并在新列中添加一个附加值。我需要将原始列名提升一级并添加两个新列名。

给出一个DataFrame h

>>> import pandas as pd
>>> h = pd.DataFrame({'a': [0.6, 0.4, 0.1], 'b': [0.2, 0.4, 0.7]})
>>> h
      a    b
0   0.6  0.2
1   0.4  0.4
2   0.1  0.7

我需要将原始列名提升一级并添加两个新列名。结果应如下所示:

>>> # some stuff...
                    a                  b
    expected received  expected received
0        0.6        1       0.2        1
1        0.4        1       0.4        1
2        0.1        1       0.7        1

我已经尝试过了:

>>> h['a1'] = [1, 1, 1]
>>> h['b1'] = [1, 1, 1]
>>> t = [('f', 'expected'),('f', 'received'), ('g', 'expected'), ('g', 'received')]
>>> h.columns = pd.MultiIndex.from_tuples(t)
>>> h
         f                 g         
  expected received expected received
0      0.6      0.2        1        1
1      0.4      0.4        1        1
2      0.1      0.7        1        1

这只会重命名列,但无法正确对齐它们。我认为问题在于a1b1expectedreceived列之间没有链接。

如何将原始列名提升一级并添加两个新列名?

1 个答案:

答案 0 :(得分:1)

我将concatkeys一起使用swaplevel

h1=h.copy()
h1[:]=1
pd.concat([h,h1],keys=['expected', 'received'],axis=1).\
    swaplevel(0,1,axis=1).\
      sort_index(level=0,axis=1)
Out[233]: 
         a                 b         
  expected received expected received
0      0.6      1.0      0.2      1.0
1      0.4      1.0      0.4      1.0
2      0.1      1.0      0.7      1.0