-1

I have a Numpy array like this:

   [[ a, b, c]
    [ d, d, e]
    [ d, f, g ]]  

How would I go about replacing every instance of char d in this 2d array while keeping the shape of the array? Assuming temp is our 2d array, I tried this but it did not work:

for i in range(len(temp)):
        temp[i].replace('d','')
waleed khalid
  • 67
  • 1
  • 1
  • 6

2 Answers2

1

Assuming temp as a numpy array, try update with indexing

temp[temp=='d'] = ''
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
0

You can just use slice it on a boolean index and set the value.

import numpy as np

x = np.array([[ 'a', 'b', 'c'],
    [ 'd', 'd', 'e'],
    [ 'd', 'f', 'g' ]]
)

x[x=='d'] = 'z'
James
  • 32,991
  • 4
  • 47
  • 70