-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','') 
2
  • Please provide a minimal reproducible example. Don't be lazy. Commented Apr 28, 2021 at 21:32
  • @juanpa.arrivillaga I think what OP gave there is an MRE already. The attempted code didn’t run for the obvious reason, but IMHO, that’s good enough. Commented Apr 28, 2021 at 21:35

2 Answers 2

1

Assuming temp as a numpy array, try update with indexing

temp[temp=='d'] = '' 
Sign up to request clarification or add additional context in comments.

Comments

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' 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.