df.at[0,'Initial'] = 10000
df.loc[1:99,'Initial'] = df['Initial'].shift(1) + (df['Initial'].shift(1) * df['Pct Change'])
Initial
10000.000000
10063.995538
NaN
NaN
NaN...
嗨大家用这段代码我创建了10000的起始值,它应该添加前一个值+(上一个值* Pct更改)
然而,它在第二行之后继续产生NaN
有什么建议吗?
Initial Pct Change
10000.000000 -0.000710
10063.995538 0.006400
NaN 0.002827
NaN 0.009148
NaN 0.000694
我喜欢Initial中的3行是10063.995538 +(10063.995538 * 0.002827) 但它正在生产NaN 我想知道这个语法df.loc [1:99,'Initial']是不是错了?
我只能通过这样做来正确填充初始值:
df.at[0,'Initial'] = 10000
df.loc[1:,'Initial'] = df['Initial'].shift(1) + (df['Initial'].shift(1) * df['Pct Change'])
df.loc[2:,'Initial'] = df['Initial'].shift(1) + (df['Initial'].shift(1) * df['Pct Change'])
df.loc[3:,'Initial'] = df['Initial'].shift(1) + (df['Initial'].shift(1) * df['Pct Change'])
`....`
但为什么df.loc [1:99,'Initial']不起作用?
答案 0 :(得分:1)
当涉及到这种操作时,你会更好地进行for循环,即
df.loc[0,'Initial'] = 10000
for i in range(1,len(df)):
df.loc[i,'Initial'] = df.loc[i-1,'Initial'] + (df.loc[i-1,'Initial'] * df.loc[i,'Pct Change'])
Initial Pct Change 0 10000.000000 -0.000710 1 10064.000000 0.006400 2 10092.450928 0.002827 3 10184.776669 0.009148 4 10191.844904 0.000694
答案 1 :(得分:1)
无需循环,请尝试查看cumprod
df['Initial']=np.append(10000,df.iloc[1:,].PctChange.add(1).cumprod().values*10000)
df
Out[261]:
PctChange Initial
0 -0.000710 10000.000000
1 0.006400 10064.000000
2 0.002827 10092.450928
3 0.009148 10184.776669
4 0.000694 10191.844904