roschu
roschu

Reputation: 776

Remove element from 2d numpy array without loosing structure

I would like to remove a specific character from a 2d numpy array. For example:

myarr = np.array([[2,"?",5,2,3,6,8],[6,7,8,9,"?"]])

How can I delete "?" without loosing the structure of the array? My result should look like this:

 [[2,5,2,3,6,8]
  [6,7,8,9]]

(I am using Python 3.4.2 and numpy 1.9 on a Win7 machine)

Upvotes: 0

Views: 2025

Answers (2)

hpaulj
hpaulj

Reputation: 231385

myarr = np.array([[2,"?",5,2,3,6,8],[6,7,8,9,"?"]]) produces

array([[2, '?', 5, 2, 3, 6, 8], [6, 7, 8, 9, '?']], dtype=object)

That is an array of 2 items of type object. There isn't an 'structure'. This is basically the same as a list of lists

mylist = [[2, '?', 5, 2, 3, 6, 8], [6, 7, 8, 9, '?']]

A simple way of removing the '?' is:

for l in mylist:
    l.remove('?')

But this raises a ValueError if there isn't any '?' in the sublist, and does not remove all if there is more than one. Both of those faults could be fixed by writing a small function that counts the number of occurrences, and removes the right number. Can you handle that function?

So the problem comes down to removing selected elements from a list of lists (or array of lists).


the 'remove all' function is simpler than I thought:

 def remove_all(a,x):
     while x in a:
         a.remove(x)
 for a in myarr:
     a.remove_all('?')

Upvotes: 2

perimosocordiae
perimosocordiae

Reputation: 17797

Numpy arrays must be "rectangular", that is, all rows/columns must have the same length. Your example looks like you need a "jagged array", which numpy doesn't support.

If this is just a case of poorly-picked example, you can remove the ? elements by selecting all non-? elements:

result = myarr[myarr!='?']

Upvotes: 2

Related Questions