简体   繁体   English

如何有效地将数据框中特定列中的行值与其自己的滞后值相乘

[英]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]

original data 原始数据

The expected outcome 预期的结果

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.

相关问题 如何有效地为python数据帧中的每一行乘以某个值 - how to efficiently multiply certain value for each row in python dataframe 在 pandas 的 dataframe 中,如何将特定行的前列值推到后面并在其位置填充“00”? - In a dataframe in pandas, how can I push the front column value of specific row to the back and fill it with '00' in its place? 如何将一个数据框的一列的所有元素与另一个数据框中为该列指定的值相乘? - How do I multiply all elements of a column of a dataframe with value specified for that column in another dataframe? 如何确保使用2D数组创建的3x3表中的每个值在其自己的行和列中都是唯一的? - How do i make sure that every value in a 3x3 table created with 2D array is unique in its own row and column? 如何从 pandas dataframe 的行中删除特定值? - How do I remove a specific value from a row in a pandas dataframe? 如何对数据框列使用 pandas 的 df.get 函数,以便列中的每一行都保持自己的值? - How to use pandas' df.get function for a dataframe column so that each row in the column maintains its own value? 如何在特定列中按值查找 DataFrame with.loc? - How do I lookup a DataFrame with .loc by value in a specific column? 如何覆盖DataFrame中特定索引/列的值? - How do I overwrite the value of a specific index/column in a DataFrame? 当特定列包含向我发出信号应删除该行的值时,如何删除Pandas数据框中的行? - How do I delete a row in Pandas dataframe when a specific column contains a value that signals to me that the row should be deleted? 将 1 Dataframe 乘以根据其索引值选择的另一行中的一行 - Multiply 1 Dataframe by a row in another one selected based on its index value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM