0

I am trying to do a simple task in python and I cannot figure out why it is failing at the append() function.

d = pd.DataFrame([1,2,3,4,5,6,1,2,3,])
d.columns = ['prediction_num']


def cumulative_vector(df):

    vector = [0,0,0,0,0,0,0,0,0]
    vectorL = []
    for i in df.prediction_num.values:
        vector[i] += 1
        print(vector)
        vectorL.append(vector)
    df['cumulative_histogram'] = vectorL
    print(df.cumulative_histogram)
    return df

cumulative_vector(d)

When I print out the vector variable in the loop, I get the right output, which is:

[0, 1, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 1, 0, 0, 0, 0, 0, 0]
[0, 1, 1, 1, 0, 0, 0, 0, 0]
[0, 1, 1, 1, 1, 0, 0, 0, 0]
[0, 1, 1, 1, 1, 1, 0, 0, 0]
[0, 1, 1, 1, 1, 1, 1, 0, 0]
[0, 2, 1, 1, 1, 1, 1, 0, 0]
[0, 2, 2, 1, 1, 1, 1, 0, 0]
[0, 2, 2, 2, 1, 1, 1, 0, 0]

However, when I print the newly created df.cumulative_histogram column, I get this:

0    [0, 2, 2, 2, 1, 1, 1, 0, 0]
1    [0, 2, 2, 2, 1, 1, 1, 0, 0]
2    [0, 2, 2, 2, 1, 1, 1, 0, 0]
3    [0, 2, 2, 2, 1, 1, 1, 0, 0]
4    [0, 2, 2, 2, 1, 1, 1, 0, 0]
5    [0, 2, 2, 2, 1, 1, 1, 0, 0]
6    [0, 2, 2, 2, 1, 1, 1, 0, 0]
7    [0, 2, 2, 2, 1, 1, 1, 0, 0]
8    [0, 2, 2, 2, 1, 1, 1, 0, 0]

Why does the column only contain the last value of vector in the loop and not each iterative value? It is appended during each loop, so I am unsure what I am missing.

connor449
  • 1,549
  • 2
  • 18
  • 49

1 Answers1

0

For loop is reusing your old vector object. This means that at each iteration you only have one list that you are modifying. Printing is working fine because you are printing the state of the list at a specific point in time.

r = []
for idx in range(0,10):
    r.append(idx)
print(r)
---
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for idx in range(0,10):
    r = []
    r.append(idx)
print(r)
---
[9]

To fix your specific case, add [:] to append a copy of the list instead.

# ...
for i in df.prediction_num.values:
    vector[i] += 1
    print(vector)
    vectorL.append(vector[:]) # vector -> vector[:] here
AdamKG
  • 13,678
  • 3
  • 38
  • 46
maksim
  • 121
  • 8