user401247
user401247

Reputation:

How do I extract a set of given columns from a numPy array?

Given a numpy array such as:

x = array([[0,  1,  2,  3],
          [ 4,  5,  6,  7],
          [ 8,  9, 10, 11],
          [12, 13, 14, 15]])

How do I form a new array composed of the first and third columns?

Upvotes: 0

Views: 76

Answers (1)

user401247
user401247

Reputation:

To extract the first and third columns from the array use the following syntax:

x[:,[0,2]]

This means, take all rows, selecting only columns 0 and 2. Note that indexing in numPy arrays starts at zero.

Upvotes: 2

Related Questions