Reputation: 1161
I have a numpy array as "data". I want to retrieve all its field except the 6th field. Currently I am using following code:
x = data[:,[0,1,2,3,4,5,7,8,9,10,11,12,13,14,15,16,17]]
which is solving the purpose, but I feel it is not the correct way.
I tried many other ways to do it, like:
x = data[:, [:,6 and 7,:]],
x = data[:, [:,6 or 7,:]], etc
but nothing seems to be working.
I also checked at several other places, but could not find any solution. Please suggest some easy way to do it.
Upvotes: 3
Views: 11328
Reputation: 1
Another way would be: (using list comprehension)
x = data[:,[index for index in range(18) if index not in [6]]]
which is easily scalable too, by adding more values.
Upvotes: 0
Reputation: 3290
For a more general answer (if you need to discard several columns):
import numpy
x = numpy.array(data)[:,range(0,6)+range(7,18)]
Upvotes: 2
Reputation: 338
How about this:
cols = range(0, 18)
cols.remove(6)
x = data[:, cols]
Upvotes: 0
Reputation: 14118
The numpy.delete
function returns a new array with the specified columns deleted, along whichever axis you want. The following is equivalent to the first statement you posted above:
x = numpy.delete(data, 6, axis=1)
Upvotes: 1