How do you extract a column from a multi-dimensional array?


Question

Does anybody know how to extract a column from a multi-dimensional array in Python?

1
201
1/31/2016 7:29:24 PM

>>> import numpy as np
>>> A = np.array([[1,2,3,4],[5,6,7,8]])

>>> A
array([[1, 2, 3, 4],
    [5, 6, 7, 8]])

>>> A[:,2] # returns the third columm
array([3, 7])

See also: "numpy.arange" and "reshape" to allocate memory

Example: (Allocating a array with shaping of matrix (3x4))

nrows = 3
ncols = 4
my_array = numpy.arange(nrows*ncols, dtype='double')
my_array = my_array.reshape(nrows, ncols)
184
9/28/2011 3:55:53 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon