Fill 2d array value in python with another 1d array per column

Question:

I have this code :

series =[]
list = type.groupby('level').size()

that list is equal = [20,40,40]

So, I want to have this loop:

series[0:3]=list

until here, its work but I want to have 2d matrix so I can save that list value for each loop.
matrixb=np.zeros((3,15))

for i in range (15):
    matrix[0:3][i]=series[0:3]

it does not work,
could not broadcast input array from shape (3,) into shape (15,)

Asked By: beginner

||

Answers:

You can’t perform such an assignment. You’d need to break them up as such:

for i in range (15):
    matrix[0][i] = series[0]
    matrix[1][i] = series[1]
    matrix[2][i] = series[2]    
Answered By: beh aaron

You can actually do it like this:

list = [20, 40, 40]
series = list.copy()
matrix = np.zeros((3,15))

for i in range(15):
    matrix[:,i] = series

which gives matrix:

[[20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20.]
 [40. 40. 40. 40. 40. 40. 40. 40. 40. 40. 40. 40. 40. 40. 40.]
 [40. 40. 40. 40. 40. 40. 40. 40. 40. 40. 40. 40. 40. 40. 40.]]
Answered By: AboAmmar
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.