Extracting specific columns in numpy array


Question

This is an easy question but say I have an MxN matrix. All I want to do is extract specific columns and store them in another numpy array but I get invalid syntax errors. Here is the code:

extractedData = data[[:,1],[:,9]]. 

It seems like the above line should suffice but I guess not. I looked around but couldn't find anything syntax wise regarding this specific scenario.

1
134
12/5/2011 2:24:20 PM

Accepted Answer

I assume you wanted columns 1 and 9? That's

data[:, [1, 9]]

Or with names:

data[:, ['Column Name1','Column Name2']]

You can get the names from data.dtype.names

233
9/21/2018 10:37:07 AM

Assuming you want to get columns 1 and 9 with that code snippet, it should be:

extractedData = data[:,[1,9]]

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