Pandas Pivot table - 如何计算以下默认比率?

Pandas Pivot table - How compute the following default ratio?

我可以用下面的代码计算违约率(例如,客户违约的百分比),得到以下输出:

import numpy as np
import pandas as pd
df = {
    'ID': [1, 2, 3, 4, 5, 6, 7],
    'Default': [1,1,0,1,0,1,0],
    'Default_Amount': [1200,2000,0,350,0,760,0],
    'Tot_Amount': [1200,2000,3400,350,10000,760,7500],
    'Time' : ['November','November','November','November','November','December','December'],
    'Class': ['A','B','A','A','B','B','A']
}
df = pd.DataFrame(df)
display(df)
df.to_excel("output.xlsx")  


print(f'Default rate in number: {df.Default.mean()}')
default_number = df.Default.mean()

pivot = np.round(pd.pivot_table(df, values=['Default'], 
                                index=['Class'], 
                                columns=['Time'], 
                                aggfunc=[np.mean],
                                fill_value=0, 
                                margins=True, 
                                margins_name='Total')
,4)
display(pivot)

现在,我面临着相同的 问题来计算数据透视表 table 中的金额违约率 (€)。总体而言,我在 Python 中使用以下内容:

print(f'Default rate in amount: {df.Default_Amount.sum()/df.Tot_Amount.sum()}')

考虑到违约率,我怎样才能得到这个table(用excel计算)?

default_amount = pd.pivot_table(df, values='Default_Amount', 
                                index=['Class'], 
                                columns=['Time'], 
                                aggfunc=np.sum,
                                fill_value=0, 
                                margins=True, 
                                margins_name='Total')

total_amount = pd.pivot_table(df, values='Tot_Amount', 
                              index=['Class'], 
                              columns=['Time'], 
                              aggfunc=np.sum,
                              fill_value=0, 
                              margins=True, 
                              margins_name='Total')

default_amount_rate = default_amount/total_amount

计算总违约金额和总金额 dfs 并计算它们之间的比率

输出:

Time    December    November    Total
Class           
A   0.00000 0.313131    0.124498
B   1.00000 0.166667    0.216301
Total   0.09201 0.209440    0.170964