使用附加标题对Python Panda表进行分组

时间:2014-08-11 17:51:44

标签: python pandas tabular heading

我有下表:

NSRCODE CM        CM % of LSA   LBH    LBH % of LSA     Total Area  Total Area % of LSA
PBL_AWI                     
BFNN    0.000000    0.000000    28.9207     0.338512    28.920700   0.323014
BONS    4.470549    1.090581    914.0085    10.698329   918.479049  10.258446
BTNI    0.000000    0.000000    3.3713      0.039461    3.371300    0.037654
BTNN    25.385459   6.192732    1974.8005   23.114736   2000.185959 22.339976
FONG    4.162559    1.015448    168.7122    1.974750    172.874759  1.930829
FONS    1.681416    0.410178    516.9960    6.051359    518.677416  5.793082
FTNI    0.000000    0.000000    31.7252     0.371339    31.725200   0.354337
FTNN    0.000000    0.000000    653.6473    7.650841    653.647300  7.300554
SONS    58.395844   14.245548   437.9098    5.125667    496.305644  5.543213
STNN    4.560384    1.112496    0.0000      0.000000    4.560384    0.050935

我正在寻找一种方法来对表格进行分组,使其看起来像这样。每两列在一个标题下分组,但NSRCODE仍按原样显示。

             Central M                Lower BH                 Summary
NSRCODE CM        CM % of LSA   LBH    LBH % of LSA     Total Area  Total Area % of LSA
PBL_AWI                     
BFNN    0.000000    0.000000    28.9207     0.338512    28.920700   0.323014
BONS    4.470549    1.090581    914.0085    10.698329   918.479049  10.258446
BTNI    0.000000    0.000000    3.3713      0.039461    3.371300    0.037654
BTNN    25.385459   6.192732    1974.8005   23.114736   2000.185959 22.339976
FONG    4.162559    1.015448    168.7122    1.974750    172.874759  1.930829
FONS    1.681416    0.410178    516.9960    6.051359    518.677416  5.793082
FTNI    0.000000    0.000000    31.7252     0.371339    31.725200   0.354337
FTNN    0.000000    0.000000    653.6473    7.650841    653.647300  7.300554
SONS    58.395844   14.245548   437.9098    5.125667    496.305644  5.543213
STNN    4.560384    1.112496    0.0000      0.000000    4.560384    0.050935

[编辑]根据@Chrisb

完成映射后我得到了这个:

Total Area   Total Area % of LSA    LBH % of LSA    LBH       CM          CM % of LSA
Summary            Summary          Lower BH        Lower BH  Central M   Central M

我试图获取当前位于第一行的第二行的标签,以便它们每组只出现一次。

例如:

        Summary
Total Area   Total Area % of LSA

其中摘要涵盖了它下面的两个值。

1 个答案:

答案 0 :(得分:1)

首先,定义您的映射:

col_mapping = {'Central M': ['CM', 'CM % of LSA'], 
               'Lower BH': ['LBH % of LSA', 'LBH'], 
                'Summary': ['Total Area', 'Total Area % of LSA']}

将其解压缩到元组列表中:

cols = [(k,v) for k,sublist in col_mapping.items() 
              for v in sublist]

指定为MultiIndex

df.columns = pd.MultiIndex.from_tuples(cols)