Numpy array, fill empty values for a single column

前端 未结 1 901
醉梦人生
醉梦人生 2021-01-20 00:01

I have an array in numpy, which was generated using np.array() from a python list so my entries are strings, but some of the values are blank. Here is an example array:

1条回答
  •  一生所求
    2021-01-20 00:10

    You can use numpy.where() to achieve this.

    In [8]: arr = numpy.array(['','1','2','3',''])
    
    In [9]: arr[numpy.where(arr=='')] = '0'
    
    In [10]: arr
    Out[10]:
    array(['0', '1', '2', '3', '0'],
          dtype='|S1')
    

    Edit As @mgilson pointed out, you could just do:

    arr[arr==''] = '0'
    

    0 讨论(0)
提交回复
热议问题