使用此代码,
import numpy as np
import pandas as pd
df = pd.DataFrame ({'Date':['2000-01-01', '2000-02-01', '2000-03-01',
'2000-01-01', '2000-02-01', '2000-03-01','2000-04-01'
],
'id':['1', '1', '1', '2', '2', '2','2'],
'bal_tot':[10, 20, 30, 40, 50, 60,70],
'bal_d1_pct': ['nan', 1, 2, 'nan', 3, 4, 5]
})
我需要为每个 id 创建一个新变量 'fore'。例如,
if Date = '2000-01-01' and id=1 then fore = 10
if Date = '2000-02-01' and id=1 then fore = 10 *(1+1/100) = 10.1
if Date = '2000-03-01' and id = 1 then fore = 10.1 * (1+2/100) = 10.302
etc.
最终数据应如下所示:
怎么做?