cad*_*die 3 pivot-table pandas
我有以下示例代码:
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)
Run Code Online (Sandbox Code Playgroud)
表格结果如下所示:
Fruit Amount
Apples 75
Kiwi 18
Oranges 15
Run Code Online (Sandbox Code Playgroud)
如何将总计添加到底部?另外,我想选择顺序,例如:
Fruit Amount
Oranges 15
Apples 75
Kiwi 15
Total 105
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?谢谢!
使用margins
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1237 次 |
| 最近记录: |