删除整列,并在列上加上条件

时间:2019-07-17 08:05:00

标签: python numpy

按照此处发布的问题和解决方案:

How could I remove the rows of an array if one of the elements of the row does not satisfy a condition?

我想问一问如何删除结合了列值的运算符条件的行。简而言之,我想删除所有第三列值不在7到15之间的行。

print (data[:,2])
to_remove = data[:,2] < 7 and  data[:,2] >= 15

不允许上述行,它会引发值错误。

  

ValueError:具有多个元素的数组的真值不明确。使用a.any()或a.all()

3 个答案:

答案 0 :(得分:1)

尝试:

to_remove = (data[:,2] < 7) | (data[:,2] > 14)

答案 1 :(得分:0)

我认为您需要这样的东西:

   result = []
   for row in arr:  # loop over the rows
      if row[2] > 7 and row[2] < 15: # this is the condition you need 
         result.append(row) # store the rows which third column is between 7 and 15 in a new array

   print(result)

答案 2 :(得分:0)

to_remove =(data [:,2] <7)| (数据[:,2]> = 15)

相关问题