如何获得pandas数据透视表中的总数

时间:2017-10-03 09:03:40

标签: pandas pivot-table

   AssetLifecycleStatus                             Balance  Deployed
       Type       Environment OS     Model                         
   SupplierA      Network 1   Win 10 Model 1                4.0      10.0   
                                     Model 2                4.0       7.0   
                  Network 2   Win 10 Model 1               10.0       5.0   
                              Win 7  Model 2                NaN       1.0   
                                     Model 3                NaN       6.0  
   SupplierB      Network 1   Win 10 Model 1                4.0       8.0   
                                     Model 2                4.0       5.0   
                  Network 2   Win 10 Model 3               10.0       5.0   
                              Win 7  Model 2                NaN       1.0   
                                     Model 3                NaN       6.0

尝试使用总计获得以下结果:

   AssetLifecycleStatus                             Balance  Deployed
       Type       Environment OS     Model                         
   SupplierA      Network 1   Win 10 Model 1                4.0      10.0   
                                     Model 2                4.0       7.0   
                  Network 2   Win 10 Model 1               10.0       5.0   
                              Win 7  Model 2                NaN       1.0   
                                     Model 3                NaN       6.0  
   SupplierB      Network 1   Win 10 Model 1                4.0       8.0   
                                     Model 2                4.0       5.0   
                  Network 2   Win 10 Model 3               10.0       5.0   
                              Win 7  Model 2                NaN       1.0   
                                     Model 3                NaN       6.0
                                       Total               36.0      54.0  

    filter1 = df[(df["Type"]!="")]
    table1 = pd.pivot_table(filter1,index=                
    ["Type","Env","OperSys","Model"],columns=
    ["AssetLifecycleStatus"],values="Serial         
    Number",aggfunc='count',margins=True,dropna=True)
    table1 = table1.reindex(['Network 1','Network 2'], level=1)
    table1 = table1.reindex_axis(['Balance','Deployed], axis=1)
    table1.index = table1.index.set_names('Environment', level=1)
    table1.index = table1.index.set_names('OS', level=2)

pandas和python相当新鲜。如何获取数据透视表底部的总数。尝试连续但结果不如预期。索引1,2,3将成为单个行项目。

1 个答案:

答案 0 :(得分:1)

您可以将元组转换为列表,因为元组是不可变的,可以将4.th元素更改为Total,然后按setting with enlargement添加新行:

print (df.index[-1])
('SupplierB', 'Network 2', 'Win 7', 'Model 3')

idx = list(df.index[-1])
idx[3] = 'Total' 
print (idx)
['SupplierB', 'Network 2', 'Win 7', 'Total']

df.loc[tuple(idx)] = df.sum()

print (df)
                                      Balance  Deployed
Type      Environment OS     Model                     
SupplierA Network 1   Win 10 Model 1      4.0      10.0
                             Model 2      4.0       7.0
          Network 2   Win 10 Model 1     10.0       5.0
                      Win 7  Model 2      NaN       1.0
                             Model 3      NaN       6.0
SupplierB Network 1   Win 10 Model 1      4.0       8.0
                             Model 2      4.0       5.0
          Network 2   Win 10 Model 3     10.0       5.0
                      Win 7  Model 2      NaN       1.0
                             Model 3      NaN       6.0
                             Total       36.0      54.0

您也可以使用:

a,b,c,d = df.index[-1]
df.loc[(a,b,c,'Total')] = df.sum()