1

I would like to do the following in python

Basically I have a table as following with y = 0.5

A | B | Result from column A
---
1   2   0.5
---
2   4   0.5*(0.5+ 2)
---
3   5   ...
---
4   4   ...
---

So far I have a loop, which is not efficient, I would like a vectorised way of doing it :

X = np.zeros((len(df),))
for i, (_, row) in enumerate(df.iterrows()):
    if i == 0:
        continue
    X[i] = y*(X[i - 1] +  row['A'])

Can you please help me on the above

Thank you

cs95
  • 379,657
  • 97
  • 704
  • 746
Brucewayne
  • 21
  • 2
  • Post your code showing what you have done – Sreeram TP Jul 18 '18 at 12:58
  • 1
    Welcome to [Stack Overflow!](https://stackoverflow.com) Please take the [tour](https://stackoverflow.com/tour), have a look around, and read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). Also like @SreeramTP said, you should share what you have tried. – Cezar Cobuz Jul 18 '18 at 13:01
  • Thank you for your messages, Please find my code above, sorry for that. Thank you – Brucewayne Jul 18 '18 at 13:06
  • I'm not sure you can vectorized here because the Xn+1 depend on the Xn result. – ymmx Jul 18 '18 at 13:24
  • Yeah, I need some kind of rolling apply, taking the previous result. – Brucewayne Jul 18 '18 at 13:26
  • The for loop could be the better choice in this case due to result dependancy. Using rolling imply knowing all data to which the function will be used on. that is not your case. – ymmx Jul 18 '18 at 13:31
  • You cannot vectorize your iteration expression. But, since this is a numerical problem, I'd try running your loop using [`numba`](https://numba.pydata.org/). You need to [convert your `pandas` dataframe to a `numpy.ndarray`](https://stackoverflow.com/questions/13187778/convert-pandas-dataframe-to-numpy-array) though, because `numba` cannot work with dataframes. – Jan Christoph Terasa Feb 17 '19 at 23:01

1 Answers1

1

My guess is that you should use the Pandas rolling method joins with lambda expression, something similar to this:

X = [your_Pandas_Serie]
rolling_window = X.rolling([the_length_of_your_Serie])

rolling_window.apply(lambda x: [your_function])

Check out with simple rolling method like mean() to be familiar and get used of lambda expression before implementing your final solution.

I used the same kind of process in trading software.