Reputation: 64
I'm trying to fill an empty array with the shape that I want from a list data. I am using a for loop to do so, but it is only reading the first values and filling the whole array with them.
What I want is to fill the array every twelve numbers. For example:
list: [1,2,3, ... , 24]
array = [[1,2,...,12], [13,14,...,24]]
Here is my code:
SSTA_12_data = []
for i in range(0,len(SSTA_12)):
SSTA_12_data.append(SSTA_12["ANOM1+2"][i])
np.shape(SSTA_12_data)
(461,)
Prueba_a = np.empty(shape=(len(Años),12)) (Años has len 39)
Prueba_a[:] = np.nan
for i in range(0,39):
for j in range(0,12):
Prueba_a[i,j] = SSTA_12_data[j]
and this is what I am getting:
array([[-0.17, -0.58, -1.31, -0.97, -0.23, 0.07, 0.87, 1.1 , 1.44,
2.12, 3. , 3.34],
[-0.17, -0.58, -1.31, -0.97, -0.23, 0.07, 0.87, 1.1 , 1.44,
2.12, 3. , 3.34],
[-0.17, -0.58, -1.31, -0.97, -0.23, 0.07, 0.87, 1.1 , 1.44,
2.12, 3. , 3.34],
[-0.17, -0.58, -1.31, -0.97, -0.23, 0.07, 0.87, 1.1 , 1.44,
2.12, 3. , 3.34], ...]
Upvotes: 0
Views: 366
Reputation: 7211
If you already using numpy, you can consider using reshape. For example, if SSTA_12_data.shape
is (120,)
, i.e. a 1D numpy array, then SSTA_12_data.reshape((-1,12))
will be of shape (10,12)
. In code, you can try with:
SSTA_12_data = []
for i in range(0,len(SSTA_12)):
SSTA_12_data.append(SSTA_12["ANOM1+2"][i])
padsize = 12 - (len(SSTA_12_data) % 12)
SSTA_12_data = np.array(SSTA_12_data)
Prueba_a = np.pad(SSTA_12_data, (0, padsize)).reshape((-1,12))
Upvotes: 1