python - 多索引情况下每个索引的列总和

标签 python pandas numpy

我有一张这样的 table ,这基本上是我所拥有的 table 的微型版本。

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  

搜索并尝试了很多东西,如果有人能提供帮助,我会很高兴,

最好, 安莫尔·古普塔。

编辑:

创建上表的代码:

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

最佳答案

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

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

关于python - 多索引情况下每个索引的列总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54080949/

相关文章:

python - 自定义 matplotlib cmap

python - Matplotlib 归一化颜色条 (Python)

python - 是否可以在 python 中将 xticks 的文本包装在 matplotlib 中?

python - DataFrame 值以开头

Python - 设置 .pop() 行为

python - 如何将assertSequenceEqual与pandas系列一起使用?

python - 在 matplotlib 和 pandas 中组合和重新定位两个图表的图例的困难

python - 如何操作 numpy.loadtxt 后的数据?

python - 如何同时运行 PHP 和 Web.py

python - Tkinter 字体大小仅影响 Raspberry pi 上字母之间的间隙