I’m trying to translate a technical analysis operator from another proprietary language to python as using dataframes, but I got stuck on a problem that seems rather simple, but I can’t get to solve the pandas way. To simplify the problem let’s have the example of this dataframe:

```
d = {'value1': [0,1,2,3], 'value2': [4,5,6,7]}
df = pd.DataFrame(data=d)
```

which result in the following dataframe:

What I want to achieve is this:

which in Pseudocode I would achieve in the following way:

```
value1 = [0,1,2,3]
value2 = [4,5,6,7]
result = []
for i in range(len(value1)):
calculation = value1[i] * value2[i]
lookback = value1[i]
for j in range(lookback):
calculation -= value2[j]
result[i] = calculation
```

How would I tackle a this in a dataframe context? Because the only similar approach that I found in the documentation is the usage of https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html# but there is no mentioning of interacting/manipulating the series contained in the columns/rows.

### Answer

```
df['result'] = df.value1 * df.value2 - (df.value2.cumsum() - df.value2)
df
```

**Output**

```
value1 value2 result
0 0 4 0
1 1 5 1
2 2 6 3
3 3 7 6
```

**Explanation**

We are calculating cumulative sum for value2 and subtracting the current value2 which in total is subtracted by the product of value1 and value2.