简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM