CarterB
CarterB

Reputation: 542

Select specific column from list of np.arrays

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.

Upvotes: 0

Views: 873

Answers (2)

Akshay Sehgal
Akshay Sehgal

Reputation: 19322

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]])

Upvotes: 1

Andy
Andy

Reputation: 381

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]

Upvotes: 1

Related Questions