0

I've Dataframe like this:

  DP 1    DP 2    DP 3    DP 4    DP 5    DP 6    DP 7    DP 8    DP 9   DP 10
 2.720   1.748   1.462   1.228   1.152   1.055   1.063   1.080   1.001  
 4.361   1.641   1.430   1.117   1.181   1.018   1.052   1.096      
 3.345   1.840   1.505   1.262   1.115   1.095   1.053          
 2.663   1.748   1.352   1.256   1.051   1.119              
 3.494   1.907   1.506   1.108   1.091                  
 3.104   1.559   1.458   1.092                      
 2.978   1.484   1.374                          
 3.066   1.695                              
 2.876                                                          
 1.179   1.699   1.439   1.179   1.116   1.073   1.055   1.089   1.001   1.000 

I've to find this :

 4.680   3.971   2.337   1.624   1.377   1.234   1.150   1.090   1.001   1.000 

By the formula: i've to find the product of last row first element and rest of element,i.e: product(1.179 to 1.000)=4.680 then next product(1.699 to 1.000)=3.971 and so on...

PRODUCT(first:last)

PRODUCT(second:last)

PRODUCT(third:last)

so on...

1 Answers1

1

Assuming your dataframe already has that last row, such that df.iloc[[-1]] is:

 DP 1   DP 2   DP 3   DP 4   DP 5   DP 6   DP 7   DP 8   DP 9  DP 10
1.179  1.699  1.439  1.179  1.116  1.073  1.055  1.089  1.001    1.0

Reverse the series and take the cumulative product:

df.iloc[-1][::-1].cumprod()
# output
DP 10    1.000000
DP 9     1.001000
DP 8     1.090089
DP 7     1.150044
DP 6     1.233997
DP 5     1.377141
DP 4     1.623649
DP 3     2.336431
DP 2     3.969596
DP 1     4.680154

And to get it back in the original order, reverse the result:

df.iloc[-1][::-1].cumprod()[::-1]
# output
DP 1     4.680154
DP 2     3.969596
DP 3     2.336431
DP 4     1.623649
DP 5     1.377141
DP 6     1.233997
DP 7     1.150044
DP 8     1.090089
DP 9     1.001000
DP 10    1.000000

(These numbers appear slightly different because you've rounded them in your question.)

aneroid
  • 12,983
  • 3
  • 36
  • 66
  • Thank you..! I want in a row . – Michael Scofield Apr 06 '21 at 13:22
  • 1
    I don't think you should add it in a row but - the result above is a Series, so to add it as a row, do: `result = df.iloc[-1][::-1].cumprod()[::-1]` and then `df = df.append(result, ignore_index=True)` – aneroid Apr 06 '21 at 13:39