I have a Csv file with a calculation that looks something like this:
Average Value |
---|
2 |
5 |
9 |
20 |
3 |
2 |
4 |
I want to make a calculation in a new column using the formula "Y = (2/(N 1))*X (1-(2/(N 1)))*Y1 "
Where N is the period that is given when calling the class. Here N will be 10
X = Average Value
Y1 = previous calculation of Y
I was doing something like
df['Y'] = ((2//(N 1))*df.Average Value (1-(2//(N 1)))).cumsum().fillna(0)
But it doesn't work
Desired result
Average Value | Y |
---|---|
2 | 1.18 |
5 | 1.84 |
9 | 3.14 |
20 | 6.20 |
3 | 5.61 |
2 | 4.95 |
4 | 4.77 |
CodePudding user response:
Numba solution - is possible count by previous values:
from numba import jit
@jit(nopython=True)
def f(a, N):
x = (2/(N 1))
y = 1-(2/(N 1))
d = np.empty(a.shape)
d[0] = a[0] * x y
for i in range(1, a.shape[0]):
d[i] = a[i] * x d[i-1] * y
return d
df['Y'] = f(df['Average Value'].to_numpy(), 10)