Pandas 为层次索引的内层增加价值

eri*_*c s 6 python multi-index dataframe pandas

我有一个带有分层索引(MultiIndex)的 Pandas DataFrame。我通过对“cousub”和“year”的值进行分组来创建此数据框。

annualMed = df.groupby(["cousub", "year"])[["ratio", "sr_val_transfer"]].median().round(2)
print annualMed.head(8)    

                      ratio  sr_val_transfer
cousub          year                        
Allen Park city 2013   0.51          75000.0
                2014   0.47          85950.0
                2015   0.47          95030.0
                2016   0.45         102500.0
Belleville city 2013   0.49         113900.0
                2014   0.55         114750.0
                2015   0.53         149000.0
                2016   0.48         121500.0    
Run Code Online (Sandbox Code Playgroud)

我想在“年”级别添加一个“总体”值,然后我可以根据单独的“cousub”分组(即排除“年”)填充值。我希望结果如下所示

                      ratio  sr_val_transfer
cousub          year                        
Allen Park city 2013   0.51          75000.0
                2014   0.47          85950.0
                2015   0.47          95030.0
                2016   0.45         102500.0
             Overall   0.50          90000.0
Belleville city 2013   0.49         113900.0
                2014   0.55         114750.0
                2015   0.53         149000.0
                2016   0.48         121500.0 
             Overall   0.50         135000.0
Run Code Online (Sandbox Code Playgroud)

如何将此新项目添加到多重索引的“年”级别?

mir*_*ulo 3

如果您只想显式添加这两列,则只需使用 指定所有 MultiIndex 级别即可loc

df.loc[('Allen Park city', 'Overall'), :] = (0.50, 90000.)
df.loc[('Belleville city', 'Overall'), :] = (0.50, 135000.)
Run Code Online (Sandbox Code Playgroud)

但是,如果您有一个完整的城市列表,并且想要为其添加此行,那么这会有点乏味。也许您可以使用另一个带有一些索引操作的值的appendDataFrame 。overall

(df.reset_index()
   .append(pd.DataFrame([['Allen Park city', 'Overall', 0.5, 90000.], 
                         ['Belleville city', 'Overall', 0.5, 135000.]], 
                         columns=list(df.index.names) + list(df.columns)))
   .set_index(df.index.names)
   .sort_index())
Run Code Online (Sandbox Code Playgroud)

演示

方法1(较小的情况)

>>> df.loc[('Allen Park city', 'Overall'), :] = (0.50, 90000.)

>>> df.loc[('Belleville city', 'Overall'), :] = (0.50, 135000.)

>>> df.sort_index()

                         ratio  sr_val_transfer
cousub          year                           
Allen Park city 2013      0.51          75000.0
                2014      0.47          85950.0
                2015      0.47          95030.0
                2016      0.45         102500.0
                Overall   0.50          90000.0
Belleville city 2013      0.49         113900.0
                2014      0.55         114750.0
                2015      0.53         149000.0
                2016      0.48         121500.0
                Overall   0.50         135000.0
Run Code Online (Sandbox Code Playgroud)

方法2(较大情况)

>>> (df.reset_index()
       .append(pd.DataFrame([['Allen Park city', 'Overall', 0.5, 90000.], 
                             ['Belleville city', 'Overall', 0.5, 135000.]], 
                             columns=list(df.index.names) + list(df.columns)))
       .set_index(df.index.names)
       .sort_index())

                         ratio  sr_val_transfer
cousub          year                           
Allen Park city 2013      0.51          75000.0
                2014      0.47          85950.0
                2015      0.47          95030.0
                2016      0.45         102500.0
                Overall   0.50          90000.0
Belleville city 2013      0.49         113900.0
                2014      0.55         114750.0
                2015      0.53         149000.0
                2016      0.48         121500.0
                Overall   0.50         135000.0
Run Code Online (Sandbox Code Playgroud)