简体   繁体   English

如何根据计算的同一列中的先前值计算 pandas 列?

[英]How to calculate a pandas column based on the previous value in the same column that is calculated?

I want to achieve the following table, where the values in "md" needs to be calculated:我想实现下表,其中需要计算“md”中的值:

   msotc   md
0      0    0
1      1    1
2      2    3
3      3    7
4      4   15
5      5   31
6      6   63
7      7  127

Given:鉴于:

  • The total rows is based on a given value (msotc + 1)总行数基于给定值 (msotc + 1)
  • The first value in column "md" needs to be 0 “md”列中的第一个值需要为 0
  • The value for row[1] to row[-1] are calculate based in formula: (prev_md * soss) + sopd row[1] 到 row[-1] 的值根据公式计算:(prev_md * soss) + sopd

Solutions (I think):解决方案(我认为):

  • Create a new column with a formula使用公式创建新列
  • Create an empty column "md" with the value 0 on index[0] and calculate the other rows在 index[0] 上创建一个值为 0 的空列“md”并计算其他行
import pandas as pd
import numpy as np

msotc = 7
sopd = 1 # (= not fixed, could be eg. 0.5)
soss = 2 # (= not fixed, could be eg. 1.05)

arr = [np.NaN] * (msotc + 1)
arr[0] = 0

data = {
    "msotc": range(0, msotc + 1, 1),
    "md": arr
}

df = pd.DataFrame(
    data=data
)

# df["md"] = (df["md"].shift(1) * soss) + sopd <- This doesn't work

You can use math to convert your formula into a geometric series.您可以使用数学将公式转换为几何级数。

md[n] = md[n-1]*soss + sopd

expressed in terms of md[0] and using the formula for the sum of powers:md[0]表示并使用幂和的公式:

md[n] = md[0]*soss**(n-1) + sopd * (soss**n - 1)/(soss-1)

Thus no need to loop, you can vectorize:因此不需要循环,你可以矢量化:

msotc = 7
sopd = 1
soss = 2
md0 = 0

n = np.arange(msotc+1)
df = pd.DataFrame({'msotc': n, 'md': md0*soss**np.clip(n-1, 0, np.inf) + sopd*(soss**n-1)/(soss-1)})

output: output:

   msotc     md
0      0    0.0
1      1    1.0
2      2    3.0
3      3    7.0
4      4   15.0
5      5   31.0
6      6   63.0
7      7  127.0

This should work fine.这应该可以正常工作。 It is quite easy to understand, just a simple loop.很容易理解,就是一个简单的循环。

arr = [0] * (msotc + 1)
for i in range(msotc + 1):
    if i == 0:
        continue
    arr[i] = (arr[i - 1] * soss) + sopd

Try this:尝试这个:

import pandas as pd

msotc = 7
sopd = 1
soss = 2

msotc_vals = []
arr = [0]
for val in range(msotc + 1):
    msotc_vals += [val]
    arr += [arr[-1] * soss + sopd]

data = {"msotc": msotc_vals, "md": arr[:-1]}

df = pd.DataFrame(data=data)

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

相关问题 如何根据条件使用计算出的新 pandas 列中的先前值? - How to use the previous value from calculated new pandas column based on conditions? 根据熊猫数据框中同一行的上一列值计算增加或减少的百分比 - Calculate the percentage increase or decrease based on the previous column value of the same row in pandas dataframe 使用先前计算的值(来自同一列)和Pandas Dataframe中另一列的值来计算值 - Calculate value using previously-calculated value (from the same column) and value from another column in a Pandas Dataframe 熊猫-根据先前计算的行值计算行值 - Pandas - calculate row value based on previous calculated row value 如何根据前 N 行计算 Pandas dataframe 列的斜率 - How to calculate slope of Pandas dataframe column based on previous N rows Pandas 根据同一列中的先前值更改值 - Pandas change values based on previous value in same column Pandas DataFrame:添加具有基于前一行计算值的新列 - Pandas DataFrame: Add new column with calculated values based on previous row 如何根据pandas中的条件添加计算列? - How to add a calculated column based on conditions in pandas? 如何根据 pandas DataFrame 中同一组内的先前值创建列? - How to create column based on previous value within the same group in pandas DataFrame? 根据之前的行在Pandas中生成列值 - Generate Column Value in Pandas based on previous rows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM