0

For a variable 'length', I need to do this:

y = some constant
df['result'] = sum(
    df['value'].shift(0) * y * 0,
    df['value'].shift(1) * y * 1,
    ...,
    df['value'].shift(length-1) * y * (length-1)
)

and fill every row like this.

I know how to do it iteratively and it will be super slow. How to do this as a vector operation?

Thomas
  • 10,933
  • 14
  • 65
  • 136
  • we really cant assume what your data looks like. why dont u share a subset of it (say 5 rows) ?note, no pics. just raw data. https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – sammywemmy Feb 21 '20 at 00:45
  • it's just floats from financial data, for example [ 1000., 1003., 1007., 1005., 1008., 1010. ]. what I need to do is, for each row, sum the x previous rows after they've been multiplied by a constant and by their offset from the current row (essentially the shift value) – Thomas Feb 21 '20 at 00:48
  • _"for each row (...) and by their offset from the current row"_. Each row's offset from the current row is zero – Tomasz Bartkowiak Feb 21 '20 at 01:13
  • for example, for length = 3, if we're on row 10, the value would be: sum(data[10]*y*0 + data[9]*y*1 + data[8]*y*2) – Thomas Feb 21 '20 at 01:16

0 Answers0