You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python摊销表中添加累计利息列?

解决摊销表添加累计利息列的问题

你的代码已经正确计算出每期应付利息(Interest列),直接用Pandas的cumsum()方法就能生成累计利息列,浮点数完全支持,不会报错。具体操作如下:

在生成amort_df的代码之后,添加一行代码:

# 添加累计利息列,保留两位小数
amort_df['Cumulative Interest'] = amort_df['Interest'].cumsum().round(2)

为什么之前的尝试可能出错?

  • 如果是在循环里手动累加浮点数,容易因精度累积导致结果偏差或报错,但Pandas的cumsum()会自动处理浮点数运算的精度问题
  • 若之前用shift()或iloc手动计算累加,可能因索引、列名匹配错误导致报错,cumsum()是针对整列的向量运算,更可靠

修改后的关键代码片段

columns=['Beginning Balance', 'Payment', 'Principal', 'Interest', 'Ending Balance', 'Monthly Chargeoff', 'NPVPER', 'NPV_Rate', 'COF', 'Prin_LLR', 'Int_LLR', 'Net Revenue']
amort_df = pd.DataFrame.from_records(records, index=rng, columns=columns).round(2)
# 新增累计利息列
amort_df['Cumulative Interest'] = amort_df['Interest'].cumsum().round(2)
amort_df

执行后,amort_df会新增Cumulative Interest列,显示每期截止到当前的累计已付利息,完美适配浮点数金额。

内容的提问来源于stack exchange,提问作者MDYETI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 15:15:39