0

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], ...]

1 Answer 1

1

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))
Sign up to request clarification or add additional context in comments.

4 Comments

I already try it, but this is the error that I get: ValueError: cannot reshape array of size 461 into shape (12)
How do you want to handle this odd ends? 461 is not divisible by 12
I know, thats the problem. Thats why I was trying to do it with for loops. The data is updated every month so some months the data will be odd or even.
Then you can try to pad the ends. Updated the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.