当每一行与前一行相关而没有forloop时,数据帧列将更新

2024-03-29 10:11:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个1.数据帧包含以下列: “日期”、“付款”、“本金”、“利息”和“余额”,其中“日期”是索引。 我需要计算所有日期中的所有列,其中一部分列的计算是基于另一列的,另一部分列的计算是基于前一行余额的任何行的计算。 在这种情况下是否可以避免使用for循环? 谢谢。你知道吗

谢谢你们的要求!我有一段代码(我是python的新手):

from decimal import Decimal as dec
from pandas import Series, DataFrame
import numpy as np

# Some given constants:
FLAG = True
sz = 4
K = 2
N = sz - 1

# DataFrame object definition:
_dates = [date(2012, m, 15) for m in range(1, 13)]
s = Series(range(len(_dates)), index=_dates, name='num', dtype=Decimal)
s.index.names = ['_dates']

df1 = DataFrame(s)
df1['pir'] = np.full(sz, dec('0.01'), dtype=dec)
df1['repayment'] = np.full(sz, dec('0'), dtype=dec)
df1['repayment'][3] = dec('0.3')
df1['indexation'] = [dec('1'), dec('1.01'), dec('1.01'), dec('1.01125'), dec('1.01125')]

df2 = DataFrame(s)
df2['interest'] = np.full(sz, dec('0'), dtype=dec)
df2['principal'] = np.full(sz, dec('0'), dtype=dec)
df2['payment'] = np.full(sz, dec('0'), dtype=dec)
df2['balance'] = np.full(sz, dec('1'), dtype=dec)

# The relevant calculation script:
cumulative_indexation = np.cumprod(df1.indexation.values)
for pmt in range(1, sz):
    beginning_balance = df2.balance[pmt-1] * df1.indexation[pmt]
    df2.interest[pmt] = df1.pir[pmt] * beginning_balance
    if pmt >= K:
        df2.principal[pmt] = np.min([beginning_balance-df1.repayment[pmt], cumulative_indexation[pmt]/N])
        df2.payment[pmt] = df2.principal[pmt] + df2.interest[pmt]
    else:
        df2.payment[pmt] = df2.principal[pmt] + df2.interest[pmt]*FLAG
    df2.balance[pmt] = (beginning_balance + df2.interest[pmt]) - (df2.payment[pmt] + df1.repayment[pmt])

Tags: principaldataframenpdecfulldatesdf1df2