在多索引数据帧上作为新列执行计算

Perform a calculation as a new column on multiindex dataframe

我有以下多索引数据框:

                                            MSISDN_COUNT
NAME            DIM1    SEGMENT 
JAN22           N       Detractor           307
                        Passive             198
                        Promoter            1256
                Y       Detractor           237
                        Passive             161
                        Promoter            1096
FEB22           N       Detractor           313
                        Passive             222
                        Promoter            1260
                Y       Detractor           261
                        Passive             169
                        Promoter            1155

我想创建一个新列,将为每个 NAME 和每个 DIM1.

计算

计算是: ((Promoter - Detractor) / (Promoter + Passive + Detractor) )* 100

所以预期的输出是

                                            MSISDN_COUNT    OUT
NAME            DIM1    SEGMENT 
JAN22           N       Detractor           307             53.88
                        Passive             198
                        Promoter            1256
                Y       Detractor           237             57.49
                        Passive             161
                        Promoter            1096
FEB22           N       Detractor           313             52.76
                        Passive             222
                        Promoter            1260
                Y       Detractor           261             56.4
                        Passive             169
                        Promoter            1155

我不知道如何使用多级索引数据框执行此操作。

感谢任何帮助!

DataFrame.xs 用于 select 3 级数据帧,应用您的公式,添加值为 Detractor 的第三级 SEGMENT 并返回:

Promoter = df.xs('Promoter', level=2)
Detractor = df.xs('Detractor', level=2)
Passive = df.xs('Passive', level=2)

s = ((Promoter - Detractor) / (Promoter + Passive + Detractor) )* 100

df['OUT'] = s.assign(SEGMENT='Detractor').set_index('SEGMENT', append=True)
print (df)
                      MSISDN_COUNT        OUT
NAME  DIM1 SEGMENT                           
JAN22 N    Detractor           307  53.889835
           Passive             198        NaN
           Promoter           1256        NaN
      Y    Detractor           237  57.496653
           Passive             161        NaN
           Promoter           1096        NaN
FEB22 N    Detractor           313  52.757660
           Passive             222        NaN
           Promoter           1260        NaN
      Y    Detractor           261  56.403785
           Passive             169        NaN
           Promoter           1155        NaN

详情:

print (s)
            MSISDN_COUNT
NAME  DIM1              
JAN22 N        53.889835
      Y        57.496653
FEB22 N        52.757660
      Y        56.403785
      
print (s.assign(SEGMENT='Detractor').set_index('SEGMENT', append=True))
                      MSISDN_COUNT
NAME  DIM1 SEGMENT                
JAN22 N    Detractor     53.889835
      Y    Detractor     57.496653
FEB22 N    Detractor     52.757660
      Y    Detractor     56.403785