Performance Zone is brought to you in partnership with:

Mark is a graph advocate and field engineer for Neo Technology, the company behind the Neo4j graph database. As a field engineer, Mark helps customers embrace graph data and Neo4j building sophisticated solutions to challenging data problems. When he's not with customers Mark is a developer on Neo4j and writes his experiences of being a graphista on a popular blog at http://markhneedham.com/blog. He tweets at @markhneedham. Mark is a DZone MVB and is not an employee of DZone and has posted 522 posts at DZone. You can read more from them at their website. View Full User Profile

Python/Numpy: Selecting a Specific Column in a 2D Array

01.29.2013
| 2880 views |
  • submit to reddit

I’ve been playing around with numpy this evening in an attempt to improve the performance of a Travelling Salesman Problem implementation and I wanted to get every value in a specific column of a 2D array.

The array looked something like this:

>> x = arange(20).reshape(4,5)
>>> x
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])

I wanted to get the values for the 2nd column of each row which would return an array containing 1, 6, 11 and 16.

For some reason I was expecting it to be quite complicated but in fact we can do this by using matrix style syntax like so:

>>> x[:, 1]
array([ 1,  6, 11, 16])

Here we are first saying that we want to return all the rows by specifying ‘:’ and then the ’1′ indicates that we only want to return the column with index 1.

If we wanted to return a specific row as well then we’d specify a value before the comma and it’d be a standard 2D array value lookup:

>> x[2,1]
11

or

>> x[2][1]
11

Published at DZone with permission of Mark Needham, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)