MultIndex填写数据帧

时间:2016-02-01 21:01:27

标签: python pandas

我有这样的df:

       Allotments  SH_Class  SH_Percent
       A. Annex       BNW   16.187500
       A. Annex      MTGP   1.0000000
       A. Annex    Meadow   39.687500
       A. Annex       PHP   29.687500
       A. Annex        SP   16.250000
       A. Annex     WMTGP   10.833333
       A. Annex  Woodland    5.000000
       Arnstson    Meadow   28.750000
       Arnstson       PHP   61.590909
       Arnstson        SP   18.125000
       Arnstson     WMTGP    2.500000
       Arnstson  Woodland   17.083333

我希望SH_Class对于每个唯一Allotments都相同。因此,即使Arnstson中没有MTGPBNW,我也希望它添加相应的SH_Percent零。我想要的输出是:

       Allotments  SH_Class  SH_Percent
       A. Annex       BNW   16.187500
       A. Annex      MTGP   1.0000000
       A. Annex    Meadow   39.687500
       A. Annex       PHP   29.687500
       A. Annex        SP   16.250000
       A. Annex     WMTGP   10.833333
       A. Annex  Woodland    5.000000
       Arnstson       BNW   0.0000000
       Arnstson      MTGP   0.0000000
       Arnstson    Meadow   28.750000
       Arnstson       PHP   61.590909
       Arnstson        SP   18.125000
       Arnstson     WMTGP    2.500000
       Arnstson  Woodland   17.083333

我试过这段代码:

target_ix = pd.MultiIndex.from_product([df.Allotments.unique(),
    levels='BNW', 'MTGP', 'Meadow', 'PHP', 'SP', 'WMTGP', 'Woodland'])
new_df = df.loc[target_ix]

但它不太正确。

1 个答案:

答案 0 :(得分:2)

你很亲密......

 mysql_query("set character_set_server='utf8'");
 mysql_query("set names 'utf8'");