I have a list composed of 5 pandas arrays, size 4 by 3 each. I want to change the 3rd row of them to a row of 0s, of ones, of twos, whatever is the index of the array in the list. I write the code, a for-loop, and it looks like it is working as I am printing the results:
A=pd.DataFrame(np.random.randn(4,3))
AList=5*[A]
AList[0].iloc[2,:]
for kkk0 in range(0,len(AList)):
AList[kkk0].iloc[2,:]=kkk0*np.ones((1,3))
print(AList[kkk0])
0 1 2
0 -0.168639 0.300507 2.823529
1 0.608844 0.017578 -0.342164
2 0.000000 0.000000 0.000000
3 1.664176 -0.696303 0.239165
0 1 2
0 -0.168639 0.300507 2.823529
1 0.608844 0.017578 -0.342164
2 1.000000 1.000000 1.000000
3 1.664176 -0.696303 0.239165
0 1 2
0 -0.168639 0.300507 2.823529
1 0.608844 0.017578 -0.342164
2 2.000000 2.000000 2.000000
3 1.664176 -0.696303 0.239165
0 1 2
0 -0.168639 0.300507 2.823529
1 0.608844 0.017578 -0.342164
2 3.000000 3.000000 3.000000
3 1.664176 -0.696303 0.239165
0 1 2
0 -0.168639 0.300507 2.823529
1 0.608844 0.017578 -0.342164
2 4.000000 4.000000 4.000000
3 1.664176 -0.696303 0.239165
The absurdity begins now however: When I try to see the contents of the AList after the ending of the for-loop I have that all the constituent PAndas arrays of the List have their third row as a row of 4s!
AList
Out[3]:
[ 0 1 2
0 -0.168639 0.300507 2.823529
1 0.608844 0.017578 -0.342164
2 4.000000 4.000000 4.000000
3 1.664176 -0.696303 0.239165,
0 1 2
0 -0.168639 0.300507 2.823529
1 0.608844 0.017578 -0.342164
2 4.000000 4.000000 4.000000
3 1.664176 -0.696303 0.239165,
0 1 2
0 -0.168639 0.300507 2.823529
1 0.608844 0.017578 -0.342164
2 4.000000 4.000000 4.000000
3 1.664176 -0.696303 0.239165,
0 1 2
0 -0.168639 0.300507 2.823529
1 0.608844 0.017578 -0.342164
2 4.000000 4.000000 4.000000
3 1.664176 -0.696303 0.239165,
0 1 2
0 -0.168639 0.300507 2.823529
1 0.608844 0.017578 -0.342164
2 4.000000 4.000000 4.000000
3 1.664176 -0.696303 0.239165]
Any ideas?