How to extract all columns but one from an array (or matrix) in python?

arrays, matrix, numpy, python

Solution

Since for the general case you are going to be returning a copy anyway, you may find yourself producing more readable code by using `np.delete`:

>>> a = np.arange(12).reshape(3, 4)
>>> np.delete(a, 2, axis=1)
array([[ 0,  1,  3],
       [ 4,  5,  7],
       [ 8,  9, 11]])

Problem

Given a numpy 2d array (or a matrix), I would like to extract all the columns but the i-th. E. g. from ``` 1 2 3 4 2 4 6 8 3 6 9 12 ``` I would like to have, e.g. ``` 1 2 3 2 4 6 3 6 9 ``` or ``` 1 2 4 2 4 8 3 6 12 ``` I cannot find a pythonic way to do this. I now that you can extract given columns by simply ``` a[:,n] ``` or ``` a[:,[n,n+1,n+5]] ``` But what about extracting all of them but one?

Original source

Related problems