如果列值满足特定条件,则选择行

时间:2017-05-02 11:20:25

标签: numpy

给定一个numpy数组,我想要对第二列高于/等于某个阈值的所有行进行切片。这是我目前的尝试:

import numpy as np

#inp = input("Input N : ")
#N = float(inp);

N = 5

#ids = np.arange(1, N+1, 1)
#scores = np.random.uniform(low=2.0, high=6.0, size=(N,))

ids = [ 1.,          2.,          3.,          4.,          5.,        ]
scores = [ 3.75320381,  4.32400937,  2.43537978,  3.73691774,  2.5163266, ]

ids_col = ids.copy()
scores_col = scores.copy()

students_mat = np.column_stack([ids_col, scores_col])

accepted = scores_col[scores_col[:]>=4.0]

accepted_std = students_mat[:, accepted]

print(accepted_std)

错误

>>> (executing file "arrays.py")
Traceback (most recent call last):
  File "D:\I (Blank Space)\Python\arrays.py", line 19, in <module>
    accepted = scores_col[scores_col[:]>=4.0]
TypeError: '>=' not supported between instances of 'list' and 'float'

>>> 

1 个答案:

答案 0 :(得分:1)

要回答您的初始问题,您需要将C:\test>git clone https://github.com/Microsoft/vcpkg.git vcpkg_test Cloning into 'vcpkg_test'... ... Checking out files: 100% (876/876), done. C:\test>cd vcpkg_test C:\test\vcpkg_test>powershell -exec bypass scripts\bootstrap.ps1 ids定义为scores。这将使您的代码有效,直到您尝试定义np.array

accepted_std

我认为你真正想要的是获得import numpy as np N = 5 ids = np.array([1, 2, 3, 4, 5]) scores = np.array([3.75320381, 4.32400937, 2.43537978, 3.73691774, 2.5163266]) ids_col = ids.copy() scores_col = scores.copy() students_mat = np.column_stack([ids_col, scores_col]) accepted = scores_col[scores_col[:]>=4.0] print(accepted) 高于某个阈值的所有行。为此,您可以将代码更改为:

score