0

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)

2 Answers 2

2

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('?')
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for pointing this out. I have just started with numpy and was not aware of the list/array confusion in my code.
2

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!='?']

1 Comment

If numpy is not supporting this, what does "myarr" in the above example represent and why do I get not error message when constructing an object with an np.array() command which is not supported by np?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.