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

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

Questions:

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

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

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

``````nrows = 3
ncols = 4
my_array = numpy.arange(nrows*ncols, dtype='double')
my_array = my_array.reshape(nrows, ncols)
``````

Questions:

Could it be that you’re using a NumPy array? Python has the array module, but that does not support multi-dimensional arrays. Normal Python lists are single-dimensional too.

However, if you have a simple two-dimensional list like this:

``````A = [[1,2,3,4],
[5,6,7,8]]
``````

then you can extract a column like this:

``````def column(matrix, i):
return [row[i] for row in matrix]
``````

Extracting the second column (index 1):

``````>>> column(A, 1)
[2, 6]
``````

Or alternatively, simply:

``````>>> [row[1] for row in A]
[2, 6]
``````

Questions:

If you have an array like

``````a = [[1, 2], [2, 3], [3, 4]]
``````

Then you extract the first column like that:

``````[row[0] for row in a]
``````

So the result looks like this:

``````[1, 2, 3]
``````

Questions:

check it out!

``````a = [[1, 2], [2, 3], [3, 4]]
a2 = zip(*a)
a2[0]
``````

it is the same thing as above except somehow it is neater
the zip does the work but requires single arrays as arguments, the *a syntax unpacks the multidimensional array into single array arguments

Questions:

The itemgetter operator can help too, if you like map-reduce style python, rather than list comprehensions, for a little variety!

``````# tested in 2.4
from operator import itemgetter
def column(matrix,i):
f = itemgetter(i)
return map(f,matrix)

M = [range(x,x+5) for x in range(10)]
assert column(M,1) == range(1,11)
``````

Questions:
``````[matrix[i][column] for i in range(len(matrix))]
``````

Questions:

You can use this as well:

``````values = np.array([[1,2,3],[4,5,6]])
values[...,0] # first column
#[1,4]
``````

Note: This is not working for built-in array and not aligned (e.g. np.array([[1,2,3],[4,5,6,7]]) )

Questions:

I think you want to extract a column from an array such as an array below

``````import numpy as np
A = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
``````

Now if you want to get the third column in the format

``````D=array[[3],
[7],
[11]]
``````

Then you need to first make the array a matrix

``````B=np.asmatrix(A)
C=B[:,2]
D=asarray(C)
``````

And now you can do element wise calculations much like you would do in excel.

Questions:

One more way using matrices

``````>>> from numpy import matrix
>>> a = [ [1,2,3],[4,5,6],[7,8,9] ]
>>> matrix(a).transpose()[1].getA()[0]
array([2, 5, 8])
>>> matrix(a).transpose()[0].getA()[0]
array([1, 4, 7])
``````

Questions:

Well a ‘bit’ late …

In case performance matters and your data is shaped rectangular, you might also store it in one dimension and access the columns by regular slicing e.g. …

``````A = [[1,2,3,4],[5,6,7,8]]     #< assume this 4x2-matrix
B = reduce( operator.add, A ) #< get it one-dimensional

def column1d( matrix, dimX, colIdx ):
return matrix[colIdx::dimX]

def row1d( matrix, dimX, rowIdx ):
return matrix[rowIdx:rowIdx+dimX]

>>> column1d( B, 4, 1 )
[2, 6]
>>> row1d( B, 4, 1 )
[2, 3, 4, 5]
``````

The neat thing is this is really fast. However, negative indexes don’t work here! So you can’t access the last column or row by index -1.

If you need negative indexing you can tune the accessor-functions a bit, e.g.

``````def column1d( matrix, dimX, colIdx ):
return matrix[colIdx % dimX::dimX]

def row1d( matrix, dimX, dimY, rowIdx ):
rowIdx = (rowIdx % dimY) * dimX
return matrix[rowIdx:rowIdx+dimX]
``````

Questions:

let’s say we have `n X m` matrix(`n` rows and `m` columns) say 5 rows and 4 columns

``````matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],[17,18,19,20]]
``````

To extract the columns in python, we can use list comprehension like this

``````[ [row[i] for row in matrix] for in range(4) ]
``````

You can replace 4 by whatever number of columns your matrix has.
The result is

`[ [1,5,9,13,17],[2,10,14,18],[3,7,11,15,19],[4,8,12,16,20] ]`

Questions:

Despite using `zip(*iterable)` to transpose a nested list, you can also use the following if the nested lists vary in length:

``````map(None, *[(1,2,3,), (4,5,), (6,)])
``````

results in:

``````[(1, 4, 6), (2, 5, None), (3, None, None)]
``````

The first column is thus:

``````map(None, *[(1,2,3,), (4,5,), (6,)])[0]
#>(1, 4, 6)
``````

Questions:
``````def get_col(arr, col):
return map(lambda x : x[col], arr)

a = [[1,2,3,4], [5,6,7,8], [9,10,11,12],[13,14,15,16]]

print get_col(a, 3)
``````

map function in Python is another way to go.

Questions:
``````N = len(matrix)