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

如何基于给定参数创建3年期Transaction与Revenue的Pandas DataFrame

使用Pandas生成包含3年交易与收入数据的DataFrame

已知参数

commission = 0.02  # 对应2%
growth = 0.1       # 对应10%
start_price = 100
current_transaction = 1000  # 初始交易数(作为第0年的计算基数)

计算逻辑

  • Transaction:以上一年度交易数为基数乘以(1+growth),例如第1年为 1000*(1+10%)=1100
  • Revenue:当年交易数 × commission × start_price,例如第1年为 1100*0.02*100=2200
  • 后续年份数据基于前一年的计算结果迭代生成

代码实现

import pandas as pd

# 初始化参数
commission = 0.02
growth = 0.1
start_price = 100
current_transaction = 1000

# 定义年份列表,存储计算结果
year_labels = ['Year 1', 'Year 2', 'Year 3']
transaction_list = []
revenue_list = []

# 迭代计算3年的数据
for _ in range(3):
    # 更新当年交易数
    current_transaction *= (1 + growth)
    transaction_list.append(int(current_transaction))
    # 计算当年收入
    current_revenue = current_transaction * commission * start_price
    revenue_list.append(int(current_revenue))

# 构建DataFrame
result_df = pd.DataFrame(
    data=[transaction_list, revenue_list],
    index=['Transaction', 'Revenue'],
    columns=year_labels
)

print(result_df)

输出结果

运行代码后将得到与参考数据完全一致的结果:

Year 1  Year 2  Year 3
Transaction    1100    1210    1331
Revenue        2200    2420    2662

补充说明

  • 如果需要保留小数精度,去掉代码中的int()转换即可
  • 迭代逻辑确保每一年的数据严格基于前一年结果生成,符合需求

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 19:25:23