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!

Economist_Ayahuasca
  • 1,648
  • 24
  • 33

1 Answers1

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]
Mark
  • 90,562
  • 7
  • 108
  • 148