擅长:python、mysql、java
<p>这是一个两行程序,应该可以工作。<code>loc</code>方法允许按索引对行进行子集设置,因为有一个多索引,所以我为左侧的行插入点提供一个元组。使用不带元组的“Town”,会拉取索引的所有相应级别。</p>
<p>在第二行中,我必须从<code>sum</code>中删除DataFrame的最后一行,并使用其shape属性执行此操作。</p>
<pre><code>In[1]:
table.loc[('Town Total', ''),:] = table.loc['Town'].sum()
table.loc[('Grand Total', ''),:] = table.iloc[:(table.shape[0]-1), :].sum()
In[2]:
table
Out[2]:
Amount
Account Basic Net
Currency GBP USD GBP USD
Location Employee
Airport 2 0 3000 0 2000
Town 1 0 4000 0 3000
3 5000 0 4000 0
Town Total 5000 4000 4000 3000
Grand Total 5000 7000 4000 5000
</code></pre>