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.