Pandas 表-添加总计和变更单

Pandas 表-添加总计和变更单,pandas,pivot-table,Pandas,Pivot Table,我有以下示例代码: import pandas as pd import numpy as np d = {'Fruit': ['Apples', 'Oranges', 'Apples', 'Kiwi', 'Kiwi'], 'Amount': [10, 15, 65, 5, 13]} df = pd.DataFrame(data=d) table = pd.pivot_table(df, index='Fruit', aggfunc= np.sum) 表格结果如下所示: Fr

我有以下示例代码:

import pandas as pd 
import numpy as np
d = {'Fruit': ['Apples', 'Oranges', 'Apples', 'Kiwi', 'Kiwi'], 'Amount': 
     [10, 15, 65, 5, 13]}

df = pd.DataFrame(data=d)

table = pd.pivot_table(df, index='Fruit', aggfunc= np.sum)
表格结果如下所示:

Fruit   Amount
Apples  75
Kiwi    18
Oranges 15
你如何把总数加到底部?另外,我想选择顺序,例如:

Fruit      Amount
Oranges    15
Apples     75
Kiwi       15
Total      105
我该怎么做呢?
谢谢

使用
边距

pd.pivot_table(df, index='Fruit', aggfunc= np.sum,margins = True,margins_name = 'Total')
Out[141]: 
         Amount
Fruit          
Apples       75
Kiwi         18
Oranges      15
Total       108

太好了,谢谢。手动选择水果的顺序,而不是按字母顺序,怎么样?检查一下就知道了!我使用pd.Category手动分配对水果系列进行的任何排序。非常感谢!