numpy数组,为单个列填充空值 [英] Numpy array, fill empty values for a single column

查看:188
本文介绍了numpy数组,为单个列填充空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在numpy中有一个数组,该数组是使用python列表中的np.array()生成的,因此我的输入是字符串,但是某些值是空白的.这是一个示例数组:

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', '1', '1', '1']
['1', '1', '', '1']
['1', '1', '1', '1']
['1', '', '1', '1']

没有'NaN'或'None',它为空白.我希望能够用相同的值填充特定列中的所有空白单元格.

There is no 'NaN' or 'None', it is blank. I want to be able to fill all the blank cells in a particular column with the same value.

推荐答案

您可以使用numpy.where()来实现.

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')

编辑正如@mgilson所指出的,您可以这样做:

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

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

这篇关于numpy数组,为单个列填充空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆