【发布时间】:2022-01-01 09:46:50
【问题描述】:
我正在寻找处理 Pandas DataFrame 的 Python 方式。假设我的 DataFrame 如下所示:
| Account | Stage | Outstanding | Installment | EIR |
|---|---|---|---|---|
| A | 1 | 10000 | 100 | 0.07 |
| B | 2 | 50000 | 500 | 0.04 |
| C | 3 | 10000 | 100 | 0.07 |
我正在尝试根据给定的信息分阶段制作摊销表。例如:
Account A Stage 1 will be amortized for 12 months
Account B Stage 2 will be amortized until Outstanding = 0 (or close to 0)
Account C Stage 3 will NOT be amortized
我有 SAS 代码来执行下面前面解释过的这种逻辑:
data want;
set have;
if Stage = 1 then do;
do Term = 1 to 12;
Outstanding = Outstanding - (abs(Installment) - (Outstanding * EIR / 100 / 12));
if Outstanding < 0 then delete;
output;
end;
end;
else if Stage = 2 then do;
do Term = 1 to Term;
Outstanding = Outstanding - (abs(Installment) - (Outstanding * EIR / 100 / 12));
if Outstanding < 0 then delete;
output;
end;
end;
else if Stage = 3 then do;
Outstanding = Outstanding;
output;
end;
run;
运行后代码会提供如下输出表(数字只是模型):
| Account | Stage | Outstanding | Installment | EIR | Term |
|---|---|---|---|---|---|
| A | 1 | 10000 | 100 | 0.07 | 1 |
| A | 1 | 9000 | 100 | 0.07 | 2 |
| A | 1 | 8000 | 100 | 0.07 | 3 |
| A | 1 | ... | ... | ... | ... |
| A | 1 | 2000 | 100 | 0.07 | 12 |
| B | 2 | 50000 | 500 | 0.04 | 1 |
| B | 2 | 49000 | 500 | 0.04 | 2 |
| B | 2 | 48000 | 500 | 0.04 | 3 |
| B | 2 | ... | ... | ... | ... |
| B | 2 | 125 | 500 | 0.04 | 48 |
| C | 3 | 10000 | 100 | 0.07 | 1 |
我有相同的python代码,但我认为它效率不高。我有类似的东西:
# Amortization function
def balances(rate, payment, os):
interestAmount = os * rate / 100 / 12
nextBalance = os + interestAmount - payment
return nextBalance
然后,我使用for循环逐个帐户调用函数,并使用np.repeat()重复我需要的信息。
result = []
for i, account in enumerate(df['Account']):
if i % 5000 == 0:
print(f'Calcultion account: {i}')
accountTable = df[df['Account'] == account]
rate = float(accountTable['EIR'])
payment = float(accountTable['Installment'])
amount = float(accountTable['Outstanding'])
if int(accountTable['Stage']) <= 2:
while amount > 0:
amount = balances(rate, payment, amount)
amortization.append(amount)
if amortization[-1] <= 0:
amortization.pop(-1)
amortizationTable = pd.DataFrame(np.repeat(accountTable.values, len(amortization), axis = 0), columns = accountTable.columns)
amortizationTable['Outstanding'] = amortization
amortizationTable['Term'] = amortizationTable.index + 1
result.append(amortizationTable)
与 SAS 编程相比,我发现它非常慢。任何提高速度或使其更pythonic方式的建议。
谢谢。
【问题讨论】:
-
请提供一个较小的输入/输出示例,其中包含真实值(不是模型编号)
-
“舞台”是做什么用的?
-
@AbhishekJain 只是复制行的标准 12 行或未完成的结尾
-
您确定 Stage=2 的 SAS 代码正确吗?这部分对我来说似乎有问题
do Term = 1 to Term; -
如果您有机会测试,请您接受答案
标签: python pandas dataframe sas amortization