0

I have a list of arrays, and I want to pass the first 10 columns of each array into a scaler to transform them, but not the rest of the columns as they are dummy variables.

Each individual array is 2D and contains data corresponding to a specific column.

I have tried:

list[:][:10]

But this gives me simply the first 10 arrays, rather than all of the arrays' first 10 columns.

1
  • 4
    Are all arrays the same shape? list[:] is just a slice of the list, which will give you the whole list back and then [:10] will give the first 10 elements, as observed. If you can't stack all arrays into a 3D array, you'll have to iterate through the list, i.e., [arr[:10] for arr in list]. Commented Jan 19, 2021 at 12:52

2 Answers 2

1

You can convert the outer list into a numpy array too (np.array(my_list)) and then use multidimensional indexing like: my_np_list[:, :10]

Sign up to request clarification or add additional context in comments.

Comments

1

Assuming that each array has the same length, try np.stack with indexing -

arr = [np.array([1,2,3,4]),
       np.array([4,5,6,7]),
       np.array([8,9,10,11])]

#Getting the first 2 column (:10 for first 10)
np.stack(arr)[:,:2]
array([[1, 2],
       [4, 5],
       [8, 9]])

Comments

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.