我有一个带有以下列的pandas.DataFrame: ' date',' payment',' principal',' interest'并且'平衡',其中' date'是一个索引。 我需要计算所有日期的所有列,其中根据另一列计算一部分列,以及计算与前一行的余额相关的任何行。 在这种情况下是否可以避免使用for-loop? 感谢。
P.S。谢谢你们的要求!有一段我的代码(我是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])