Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty an existing NumPy array

Now I have an numpy array X with certain column names, format and length. How can I set all the values to 0 (or empty) in this array, without deleting the format/names etc.?

like image 600
user3092887 Avatar asked Jan 16 '14 18:01

user3092887


2 Answers

Use numpy.ndarray.fill:

>>> import numpy as np
>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a.fill(0)
>>> a
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
like image 53
falsetru Avatar answered Oct 28 '22 08:10

falsetru


You can use slicing:

>>> a = np.array([[1,2],[3,4]])
>>> a[:] = 0
>>> a
array([[0, 0],
       [0, 0]])
like image 28
eskaev Avatar answered Oct 28 '22 09:10

eskaev