0

This is the code that I am working with:

import pandas as pd
z = pd.Series(data = [1,2,3,4,5,6,7], index = xrange(1,8))
array = []
for i in range(1,8):
    array.append(z[i]*2)
    print array

It does exactly what I tell it to because I can't figure out how to do a simple iteration. This is the printed output

[2]
[2, 4]
[2, 4, 6]
[2, 4, 6, 8]
[2, 4, 6, 8, 10]
[2, 4, 6, 8, 10, 12]
[2, 4, 6, 8, 10, 12, 14]

What I want is for python to use the updated value in array so the desired output would be:

[2]
[2, 4]
[2, 4, 8]
[2, 4, 8, 16]
[2, 4, 8, 16, 32]
[2, 4, 8, 16, 32, 64]
[2, 4, 8, 16, 32, 64, 128]

Thank you for your help. Edit The example I first used was too simple so please answer using the example code below

import pandas as pd
sample = pd.Series(data = [ -3.2 ,  30.66,   7.71,   9.87], index = range(0,4))
testarray = []
for i in range(0,4):
    testarray.append(100000*(1+sample.values[i]/100))
    print testarray

This produces

[96800.0, 130660.0, 107710.0, 109870.0]

When the desired numbers are: 96800 126478.88 136230.4016 149676.3423

So instead of it using 100000 I want it to use 96800 for the second iteration and so on. Thank you!

2 Answers 2

4

I think that what you're trying to do is to compute powers of two, instead of multiplications of two:

array.append(2**z[i])
Sign up to request clarification or add additional context in comments.

Comments

2

You want to use the last value of the accumulation list in the expression.

For example:

data = [ -3.2 ,  30.66,   7.71,   9.87]  # the input list for `sample`

In [686]: test=[100000]    
In [687]: for i in range(4):
    test.append(test[-1]*(1+data[i]/100))
In [688]: test
Out[688]: [100000, 96800.0, 126478.88, 136230.401648, 149676.3422906576]

I could start this with test=[], but then I'd have test whether the list was empty, and use 1000000 instead of test[-1]. So putting the 100000 in the list to start with is logically simpler.

Another option is to maintain a temporary variable that is updated each iteration:

In [689]: mult=100000
In [690]: test=[]
In [691]: for i in range(4):
    test.append(mult*(1+data[i]/100))
    mult=test[-1]

Or

mult *= (1+data[i]/100)
test.append(mult)

But since this is pandas I could also do the calculation with one vectorized call. The numpy array equivalent is:

In [697]: data_arr=np.array(data)
In [698]: np.cumprod(1+data_arr/100)
Out[698]: array([ 0.968     ,  1.2647888 ,  1.36230402,  1.49676342])

cumprod is the cumulative product (like the more common cumulative sum).


Your first example can be produced with:

In [709]: np.cumprod([2 for _ in range(7)])
Out[709]: array([  2,   4,   8,  16,  32,  64, 128])

In [710]: np.cumprod(np.ones(7,int)*2)
Out[710]: array([  2,   4,   8,  16,  32,  64, 128])

1 Comment

can you please include what you set data equal to in the first part of your anwer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.