1 回答

TA貢獻1850條經驗 獲得超11個贊
您將需要循環(huán),因為您的總數會根據前面的行而變化,然后會影響后面的行。因此,您當前的利息計算是錯誤的。
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
不幸的是,這看起來更令人困惑,但當值依賴于以前的值時就會發(fā)生這種情況。根據您的確切要求,可能有一些很好的方法可以使用數學來簡化這一過程,但除此之外,這就是您所擁有的。
我已將其直接寫入 stackoverflow,因此它可能并不完美。
添加回答
舉報