【问题标题】:replace blanks in numpy array替换numpy数组中的空格
【发布时间】:2013-11-10 09:14:34
【问题描述】:

我的 numpy 数组中的第三列是 Age。在此列中,大约 75% 的条目是有效的,而 25% 是空白的。第 2 列是性别,通过一些操作,我计算出数据集中男性的平均年龄为 30 岁。我的数据集中女性的平均年龄为 28 岁。

我想将男性的所有空白年龄值替换为 30 岁,将女性的所有空白年龄值替换为 28 岁。

但是我似乎无法做到这一点。任何人有建议或知道我做错了什么?

这是我的代码:

# my entire data set is stored in a numpy array defined as x

ismale = x[::,1]=='male'
maleAgeBlank = x[ismale][::,2]==''
x[ismale][maleAgeBlank][::,2] = 30 

无论出于何种原因,当我完成上述代码时,我输入 x 以显示数据集,即使我将它们设置为 30,空白仍然存在。请注意,我不能这样做 x[maleAgeBlank],因为该列表将包括一些女性数据点,因为尚未排除女性数据点。

有什么方法可以得到我想要的吗?出于某种原因,如果我执行x[ismale][::,1] = 1(将'male' 列设置为1),可以,但是x[ismale][maleAgeBlank][::,2] = 30 不起作用。

数组样本:

#output from typing x
array([['3', '1', '22', ..., '0', '7.25', '2'],
   ['1', '0', '38', ..., '0', '71.2833', '0'],
   ['3', '0', '26', ..., '0', '7.925', '2'],
   ..., 
   ['3', '0', '', ..., '2', '23.45', '2'],
   ['1', '1', '26', ..., '0', '30', '0'],
   ['3', '1', '32', ..., '0', '7.75', '1']], 
  dtype='<U82')

#output from typing x[0]

array(['3', '1', '22', '1', '0', '7.25', '2'], 
  dtype='<U82')

请注意,我已在上面的输出中将第 2 列更改为 0(女性)和 1(男性)

【问题讨论】:

  • 你能发一个数组的样本吗?

标签: python arrays numpy


【解决方案1】:

这个怎么样:

my_data =  np.array([['3', '1', '22', '0', '7.25', '2'],
                     ['1', '0', '38', '0', '71.2833', '0'],
                     ['3', '0', '26', '0', '7.925', '2'],
                     ['3', '0', '', '2', '23.45', '2'],
                     ['1', '1', '26', '0', '30', '0'],
                     ['3', '1', '32', '0', '7.75', '1']], 
                     dtype='<U82')

ismale = my_data[:,1] == '0'
missing_age = my_data[:, 2] == ''
maleAgeBlank = missing_age & ismale
my_data[maleAgeBlank, 2] = '30'

结果:

>>> my_data
array([[u'3', u'1', u'22', u'0', u'7.25', u'2'],
       [u'1', u'0', u'38', u'0', u'71.2833', u'0'],
       [u'3', u'0', u'26', u'0', u'7.925', u'2'],
       [u'3', u'0', u'30', u'2', u'23.45', u'2'], 
       [u'1', u'1', u'26', u'0', u'30', u'0'],
       [u'3', u'1', u'32', u'0', u'7.75', u'1']], 
      dtype='<U82')

【讨论】:

  • 完美!谢谢,很干净,可以理解。没想到 & 操作。
【解决方案2】:

你可以使用where函数:

arr = array([['3', '1', '22', '1', '0', '7.25', '2'], 
            ['3', '', '22', '1', '0', '7.25', '2']], 
           dtype='<U82')

blank = np.where(arr=='')

arr[blank] = 20

array([[u'3', u'1', u'22', u'1', u'0', u'7.25', u'2'],
       [u'3', u'20', u'22', u'1', u'0', u'7.25', u'2']], 
      dtype='<U82')

如果要更改特定列,可以执行以下操作:

male = np.where(arr[:, 1]=='') # where 1 is the column
arr[male] = 30

female = np.where(arr[:, 2]=='') # where 2 is the column
arr[female] = 28

【讨论】:

  • where 是有效的,但是当前的解决方案不检查行的性别值并更改所有空白,而不仅仅是年龄列中的空白。
  • 他不是要把年龄的空白值改成平均值吗?男性和女性的年龄列只有 1 和 2。所以他只需要两列都有 2 where
【解决方案3】:

您可以尝试以更简单的方式遍历数组。这不是最有效的解决方案,但应该可以完成工作。

for row in range(len(x)):
    if row[2] == '':
        if row[1] == 1:
            row[2] == 30
        else:
            row[2] == 28

【讨论】:

  • 使用带有 numpy 数组的 for 循环被称为废话。您通过迭代失去了 numpy 的优势。
  • @void 这很公平。我并不是说没有更好的解决方案。但如果所有 OP 关心的是快速解决这个特定任务,希望这会有所帮助。
  • 使用where 效率更高。检查我的答案。
猜你喜欢
  • 1970-01-01
  • 2015-07-29
  • 1970-01-01
  • 2016-06-16
  • 2012-06-09
  • 2013-09-11
  • 1970-01-01
  • 2013-12-10
  • 2019-09-26
相关资源
最近更新 更多