0

I have the following array:

array = array([4., 0., 2., 8., 8., 8., 8., 2., 0.])

and I would like to replace 0 by 0.5 so to get:

array = array([4., 0.5, 2., 8., 8., 8., 8., 2., 0.5])

so far I have tried:

array.replace(0.5, 0)

with little success:

AttributeError: 'numpy.ndarray' object has no attribute 'replace'

any idea on how to keep the array format but replace numbers inside it?

cheers!

0

1 Answer 1

2

You can boolean indexing to locate the items you want to replace and then just assign the value:

import numpy as np

array = np.array([4., 0., 2., 8., 8., 8., 8., 2., 0.])

array[array == 0.0] = 0.5

print(array)
# [4.  0.5 2.  8.  8.  8.  8.  2.  0.5]
Sign up to request clarification or add additional context in comments.

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.