[英]How do I multiply a row value in a specific column in a dataframe with its own lagged value efficiently
I have a dataframe with two columns 'actp' and 'modr': 'actp' contains an actual price series, 'modr' contains forecasted returns for the series. 我有一个包含两列'actp'和'modr'的数据框:'actp'包含实际价格系列,'modr'包含该系列的预测回报。 I want to create a third column 'modp' which takes the price series values when they exist (non NaN) or which multiplies the previous price series value 'actp' by 1 + log return ('modr') to generate a forecasted price value ('modp') and then multiply the lagged values of 'modp' by the return. 我想创建第三列'modp',它取价格序列值(非NaN)或将之前的价格序列值'actp'乘以1 + log return('modr')以生成预测价格值('modp')然后将'modp'的滞后值乘以返回值。 It's a simple problem and easy enough to solve using a for loop. 这是一个简单的问题,使用for循环很容易解决。 I would like to know what an efficient and elegant solution might look like as I need it to be optimised for speed and scalability. 我想知道什么是高效优雅的解决方案,因为我需要对其进行优化以提高速度和可扩展性。
I can accomplish this with a for loop, although it cureently doesn't add the actual value from 'actp' to 'modp' when 'actp' has a value in it. 我可以通过for循环完成此操作,但是当'actp'中有值时,它可能无法将'actp'的实际值添加到'modp'。
for i in range(2,5):
df['modp'].iloc[i] = df['actp'].iloc[i-1] * np.exp(df['modr'].iloc[i])
df['actp'].iloc[i] = df['modp'].iloc[i]
As Dan mentioned, you can use shift 正如Dan所说,你可以使用shift
If I understood your calculations correctly, this should be it: 如果我理解你的计算正确,那应该是这样的:
import pandas as pd
import numpy as np
df = pd.DataFrame({
'actp':[10,20,30,40],
'modr':[2,2,0,2]})
df['actp'] = df.actp.shift(1) * np.exp(df.modr)
print(df)
actp modr
0 NaN 2
1 73.890561 2
2 20.000000 0
3 221.671683 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.