如果是MultiIndex,则每个索引的列总和

时间:2019-01-07 19:56:49

标签: python pandas numpy

我有一张这样的桌子,这基本上是我所拥有桌子的缩影。

Grade                   G10     G11      G12    G13 

Depth(m)   Thickness                                

0-50        0-0.9      0.0452   NaN     0.0092  NaN 
            0.9-1.2    0.0355   0.0249  NaN     NaN 
            1.21-1.5   0.0084   0.0764  0.0066  NaN 

100-150     0-0.9      0.0340   0.0579  0.0994  0.0358  
            0.9-1.2    0.0823   0.1495  0.0877  0.0881  
            1.21-1.5   0.2296   0.1572  0.1385  0.1117  
            1.51-1.8   0.1991   0.2327  0.1597  0.1834  
            1.81-2.0   0.1047   0.0700  0.0809  0.0364  

150-200     0-0.9       NaN     0.0189  0.0163  NaN 
            0.9-1.2     NaN     0.0494  0.0168  0.0009  
            1.21-1.5    0.0039  0.0423  0.0420  0.0145  
            1.51-1.8    0.0028  0.0853  0.0179  NaN 
            1.81-2.0    NaN     0.0466  NaN     NaN 

这是我要实现的目标:

Grade                   G10     G11      G12    G13 

Depth(m)   Thickness                                

0-50        0-0.9      0.0452   NaN     0.0092  NaN 
            0.9-1.2    0.0355   0.0249  NaN     NaN 
            1.21-1.5   0.0084   0.0764  0.0066  NaN 
total(0-50)            //sum of the Grade column

100-150     0-0.9      0.0340   0.0579  0.0994  0.0358  
            0.9-1.2    0.0823   0.1495  0.0877  0.0881  
            1.21-1.5   0.2296   0.1572  0.1385  0.1117  
            1.51-1.8   0.1991   0.2327  0.1597  0.1834  
            1.81-2.0   0.1047   0.0700  0.0809  0.0364  
total(50-100)          //sum of the Grade column

150-200     0-0.9       NaN     0.0189  0.0163  NaN 
            0.9-1.2     NaN     0.0494  0.0168  0.0009  
            1.21-1.5    0.0039  0.0423  0.0420  0.0145  
            1.51-1.8    0.0028  0.0853  0.0179  NaN 
            1.81-2.0    NaN     0.0466  NaN     NaN 
total(150-200)          //sum of the Grade column

所以我想在每个成绩范围后添加新行,并计算该特定成绩范围的成绩总和。

我尝试的代码:

x=df.pivot_table(index='Depth(m)',
           margins=True,
           margins_name='Total',  
           aggfunc=sum) 
x.iloc[:,:]  

收到的输出: 这样计算总和,但不在同一列中,而是在创建一个新列,如下所示:

Grade       G10       G11    G12    G13

Depth(m)                                

0-50        0.0933  0.1152  0.0568  0.0526  
100-150     0.6766  0.7527  0.8838  0.5428  
150-200     0.0067  0.2425  0.0930  0.0154  

搜索并尝试了很多东西,如果有人可以帮助,我们将很高兴,

最好, Anmol Gupta。

编辑:

用于创建上表的代码:

p = df.pivot_table(index=['Depth(m)','Thickness'], 
columns='Grade',values="Proved Reserve",aggfunc=np.sum)

2 个答案:

答案 0 :(得分:3)

设置 (适用于其他尝试回答的人)

from numpy import nan
d = {'G10': {('0-50', '0-0.9'): 0.0452, ('0-50', '0.9-1.2'): 0.0355, ('0-50', '1.21-1.5'): 0.0084, ('100-150', '0-0.9'): 0.034, ('100-150', '0.9-1.2'): 0.0823, ('100-150', '1.21-1.5'): 0.2296, ('100-150', '1.51-1.8'): 0.1991, ('100-150', '1.81-2.0'): 0.1047, ('150-200', '0-0.9'): nan, ('150-200', '0.9-1.2'): nan, ('150-200', '1.21-1.5'): 0.0039, ('150-200', '1.51-1.8'): 0.0028, ('150-200', '1.81-2.0'): nan}, 'G11': {('0-50', '0-0.9'): nan, ('0-50', '0.9-1.2'): 0.0249, ('0-50', '1.21-1.5'): 0.0764, ('100-150', '0-0.9'): 0.0579, ('100-150', '0.9-1.2'): 0.1495, ('100-150', '1.21-1.5'): 0.1572, ('100-150', '1.51-1.8'): 0.2327, ('100-150', '1.81-2.0'): 0.07, ('150-200', '0-0.9'): 0.0189, ('150-200', '0.9-1.2'): 0.0494, ('150-200', '1.21-1.5'): 0.0423, ('150-200', '1.51-1.8'): 0.0853, ('150-200', '1.81-2.0'): 0.0466}, 'G12': {('0-50', '0-0.9'): 0.0092, ('0-50', '0.9-1.2'): nan, ('0-50', '1.21-1.5'): 0.0066, ('100-150', '0-0.9'): 0.0994, ('100-150', '0.9-1.2'): 0.0877, ('100-150', '1.21-1.5'): 0.1385, ('100-150', '1.51-1.8'): 0.1597, ('100-150', '1.81-2.0'): 0.0809, ('150-200', '0-0.9'): 0.0163, ('150-200', '0.9-1.2'): 0.0168, ('150-200', '1.21-1.5'): 0.042, ('150-200', '1.51-1.8'): 0.0179, ('150-200', '1.81-2.0'): nan}, 'G13': {('0-50', '0-0.9'): nan, ('0-50', '0.9-1.2'): nan, ('0-50', '1.21-1.5'): nan, ('100-150', '0-0.9'): 0.0358, ('100-150', '0.9-1.2'): 0.0881, ('100-150', '1.21-1.5'): 0.1117, ('100-150', '1.51-1.8'): 0.1834, ('100-150', '1.81-2.0'): 0.0364, ('150-200', '0-0.9'): nan, ('150-200', '0.9-1.2'): 0.0009, ('150-200', '1.21-1.5'): 0.0145, ('150-200', '1.51-1.8'): nan, ('150-200', '1.81-2.0'): nan}}
df = pd.DataFrame(d)
df = df.rename_axis(['Depth(m)', 'Thickness'])
df = df.rename_axis(['Grade'], axis=1)

使用groupbyset_indexsort_index

a = df.groupby(level=0).sum().assign(Thickness='total').set_index('Thickness', append=True)
pd.concat([df, a]).sort_index(0)

Grade                  G10     G11     G12     G13
Depth(m) Thickness
0-50     0-0.9      0.0452     NaN  0.0092     NaN
         0.9-1.2    0.0355  0.0249     NaN     NaN
         1.21-1.5   0.0084  0.0764  0.0066     NaN
         total      0.0891  0.1013  0.0158  0.0000
100-150  0-0.9      0.0340  0.0579  0.0994  0.0358
         0.9-1.2    0.0823  0.1495  0.0877  0.0881
         1.21-1.5   0.2296  0.1572  0.1385  0.1117
         1.51-1.8   0.1991  0.2327  0.1597  0.1834
         1.81-2.0   0.1047  0.0700  0.0809  0.0364
         total      0.6497  0.6673  0.5662  0.4554
150-200  0-0.9         NaN  0.0189  0.0163     NaN
         0.9-1.2       NaN  0.0494  0.0168  0.0009
         1.21-1.5   0.0039  0.0423  0.0420  0.0145
         1.51-1.8   0.0028  0.0853  0.0179     NaN
         1.81-2.0      NaN  0.0466     NaN     NaN
         total      0.0067  0.2425  0.0930  0.0154

答案 1 :(得分:1)

尝试一下:

total_df = df.sum(level=0).assign(Thickness='')\
             .rename(index=lambda x: x+' Total')\
             .set_index('Thickness', append=True)
pd.concat([df,total_df]).sort_index()

输出:

                             G10     G11     G12     G13
Grade Depth(m) Thickness                                
0-50           0-0.9      0.0452     NaN  0.0092     NaN
               0.9-1.2    0.0355  0.0249     NaN     NaN
               1.21-1.5   0.0084  0.0764  0.0066     NaN
0-50 Total                0.0891  0.1013  0.0158  0.0000
100-150        0-0.9      0.0340  0.0579  0.0994  0.0358
               0.9-1.2    0.0823  0.1495  0.0877  0.0881
               1.21-1.5   0.2296  0.1572  0.1385  0.1117
               1.51-1.8   0.1991  0.2327  0.1597  0.1834
               1.81-2.0   0.1047  0.0700  0.0809  0.0364
100-150 Total             0.6497  0.6673  0.5662  0.4554
150-200        0-0.9         NaN  0.0189  0.0163     NaN
               0.9-1.2       NaN  0.0494  0.0168  0.0009
               1.21-1.5   0.0039  0.0423  0.0420  0.0145
               1.51-1.8   0.0028  0.0853  0.0179     NaN
               1.81-2.0      NaN  0.0466     NaN     NaN
150-200 Total             0.0067  0.2425  0.0930  0.0154