简体   繁体   English

根据上面的行创建新的 dataframe 行

[英]Create new dataframe row based on row above

I have a dataframe with one column (Change).我有一个带有一列的 dataframe(更改)。 I would like to create NewColumn which inputs the number 60 on its first row, and for each row after that is given by 'Change' * the previous value in NewColumn + the previous value in NewColumn.我想创建NewColumn,它在第一行输入数字60,之后的每一行由'Change' * NewColumn 中的前一个值+ NewColumn 中的前一个值给出。 Resulting in the sample dataframe below导致下面的示例 dataframe

 Index    Change    NewColumn
   0       0.02       60
   1      -0.01      59.4
   2       0.05      62.37
   3       0.02      63.6174

I can achieve this by running the following loop我可以通过运行以下循环来实现这一点

df['NewColumn'] = 0.00000
for i in range(len(df)):
    if i == 0:
        df['NewColumn'][i] = 60
    else:
        df['NewColumn'][i] = df['NewColumn'][i-1] * df['Change'][i] + df['NewColumn'][i-1]

Which does work okay but over a large dataframe it is pretty slow so I'm looking for any faster way to do this.哪个确实可以,但是在大型 dataframe 上它很慢,所以我正在寻找更快的方法来做到这一点。

I would use Series.cumprod on a modified change column, then just multiply that to the start value of 60:我会在修改后的更改列上使用Series.cumprod ,然后将其乘以起始值 60:

df = pd.DataFrame(dict(Change=[0.00, -0.01, 0.05, 0.02]))
multiplier = (df.Change + 1.0).cumprod()
df['New Column'] = multiplier * 60 

df                                                                                                                     
     Change   New Column                                                                                                   
0    0.00     60.0000                                                                                                   
1   -0.01     59.4000                                                                                                   
2    0.05     62.3700                                                                                                   
3    0.02     63.6174  

(I changed the first Change value to zero, because its not clear what the first row of Change means) (我将第一个Change值更改为零,因为不清楚第一行Change的含义)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas dataframe 使用基于上述行的值创建新列 - Pandas dataframe create new columns with values based on above row Python pandas 根据上面某些行的条件在 dataframe 中创建一个新行 - Python pandas create a new row within dataframe based on a conditions on certain rows above 根据条件在 Pandas DataFrame 中创建新行 - Create new row in Pandas DataFrame based on conditions 创建一个新行,它是上述行的计算结果 - Pandas DataFrame - Create a new row that is the result of a calculation of rows above - Pandas DataFrame Pandas:根据新创建的列中的上述行创建新列 - Pandas: create new column based on above row in the newly created column 熊猫数据框应用功能基于选定的行创建新列 - Pandas dataframe apply function to create new column based on selected row 根据上一行的值在熊猫数据框中创建一个新列 - Create a new column in a pandas dataframe based on values found on a previous row Python:根据DataFrame中的列名创建新行 - Python: create new row based on column names in DataFrame 如果数据框中一行中的值以 结尾,则创建一个新行, - Create a new row if a value in a row in dataframe endswith , 根据具有特定条件的上述行中的一列的值创建新行 - pandas 或 numpy - create new rows based on values of one of the columns in the above row with specific condition - pandas or numpy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM