Retrieval of one column of an array outputs a shape of (1, 2) instead of (2,1). What am I missing?

Viewed 26

I have an array z = np.array([[1,2,3], [4,5,6]]) which has the shape (2, 3). Within one column there is one x and one y value. I want to retrieve the values of one column print(z[:, 2]. However, instead of outputting a (2,1) shape, I get a (1,2) shape.

The output I got:

[3 6]

The output I expect:

[[3]
 [6]]

The thing is, I want to add z[:, 2] to another variable of the shape (2,1). What am I missing? And how can I achieve my goal? Thanks a lot in advance.

1 Answers

You need to slice with a 2D selector:

z[:, [2]]

output:

array([[3],
       [6]])
Related