1 回答

TA貢獻(xiàn)1850條經(jīng)驗(yàn) 獲得超11個(gè)贊
您將需要循環(huán),因?yàn)槟目倲?shù)會(huì)根據(jù)前面的行而變化,然后會(huì)影響后面的行。因此,您當(dāng)前的利息計(jì)算是錯(cuò)誤的。
total = 0
cumulative_interest = 0
total_per_day = []
interest_per_day = []
cumulative_per_day = []
for day in df.itertuples():
total += day.Debit - day.Credit
interest = total * 0.11 / 365
cumulative_interest += interest
if day.IsItLastDay:
total += cumulative_interest
total_per_day.append(total)
interest_per_day.append(interest)
cumulative_per_day.append(cumulative_interest)
if day.IsItLastDay:
cumulative_interest = 0
df.Total = total_per_day
df.InterestPerDay = interest_per_day
df.InterestCumulative = cumulative_per_day
不幸的是,這看起來更令人困惑,但當(dāng)值依賴于以前的值時(shí)就會(huì)發(fā)生這種情況。根據(jù)您的確切要求,可能有一些很好的方法可以使用數(shù)學(xué)來簡(jiǎn)化這一過程,但除此之外,這就是您所擁有的。
我已將其直接寫入 stackoverflow,因此它可能并不完美。
添加回答
舉報(bào)