猿问

根据ID值删除/保留numpy数组行

我有两个numpy数组,每个数组在第0列中都有一个标识号。


每个数组的标识号都匹配的地方,我希望保持与这些ID号相关联的相应行。


如果另一个数组中有一个没有匹配ID的ID,我希望删除与该ID号关联的行,仅删除该ID号出现在该数组中的行。


阵列均按其ID号排序。


输入数组a和b以及输出数组c和d的示例可以在下面找到-请注意,数组的行数不相同(nb个a和b的实际示例要大得多-(2487,12 )&(2482,12)分别)


在:


a =

[[9.60977,  97.5,  96,    99,    100.5,  1.60]

 [9.60978,  97.5,  96,    100.5, 102,    0.31]

 [9.60979,  97.5,  96,    102,   103.5,  0.11]

 [9.60980,  97.5,  96,    103.5, 105,    0.05]

 [9.60981,  97.5,  96,    105,   106.5,  0.03]

 [9.60983,  97.5,  96,    108,   109.5,  0.01]

 [9.60984,  97.5,  96,    109.5, 111,    0.01]]


b = 

[[9.60977,  99,    100.5, 97.5,  96,     1.58]

 [9.60979,  102,   103.5, 97.5,  96,     0.11]

 [9.60980,  103.5, 105,   97.5,  96,     0.05] 

 [9.60981,  105,   106.5, 97.5,  96,     0.03]

 [9.60982,  106.5, 108,   97.5,  96,     0.02]

 [9.60984,  109.5, 111,   97.5,  96,     0.01]]

出去:


c =

[[9.60977,  97.5,  96,    99,    100.5,  1.60]

 [9.60979,  97.5,  96,    102,   103.5,  0.11]

 [9.60980,  97.5,  96,    103.5, 105,    0.05]

 [9.60981,  97.5,  96,    105,   106.5,  0.03]

 [9.60984,  97.5,  96,    109.5, 111,    0.01]]


d = 

[[9.60977,  99,    100.5, 97.5,  96,     1.58]

 [9.60979,  102,   103.5, 97.5,  96,     0.11]

 [9.60980,  103.5, 105,   97.5,  96,     0.05] 

 [9.60981,  105,   106.5, 97.5,  96,     0.03]

 [9.60984,  109.5, 111,   97.5,  96,     0.01]]

我试过在for循环中使用一对if语句,但是由于1)数组的长度不同(请参见下面的Traceback),以及2)一旦值达到被删除


for i in np.arange(0, max(len(a), len(b)), 1):

    if a[i, 0] > b[i, 0]:

        a = np.delete(a, i, 0)

    if a[i, 0] < b[i, 0]:

        b = np.delete(b, i, 0)


Traceback (most recent call last):


  File "<ipython-input-271-509fc93aea3b>", line 2, in <module>

    if a[i, 0] > b[i, 0]:


IndexError: index 4 is out of bounds for axis 0 with size 3

我也尝试了这个while循环,但是它删除了数组b中所有错误的行


n = 0

s = max(len(a), len(b))

c = np.array(())

d = np.array(())

while n != s:

还有其他更合理的方法可以使用ID号删除和附加行吗?


鸿蒙传说
浏览 116回答 1
1回答

潇湘沐

您可以np.isin用来查找每个数组中第一列中的值出现在另一个数组中第一列中的值。然后,这只是简单的索引编制问题。c = a[np.isin(a[:,0],b[:,0])]d = b[np.isin(b[:,0],a[:,0])]>>> carray([[&nbsp; 9.60977000e+00,&nbsp; &nbsp;9.75000000e+01,&nbsp; &nbsp;9.60000000e+01,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 9.90000000e+01,&nbsp; &nbsp;1.00500000e+02,&nbsp; &nbsp;1.60000000e+00],&nbsp; &nbsp; &nbsp; &nbsp;[&nbsp; 9.60979000e+00,&nbsp; &nbsp;9.75000000e+01,&nbsp; &nbsp;9.60000000e+01,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1.02000000e+02,&nbsp; &nbsp;1.03500000e+02,&nbsp; &nbsp;1.10000000e-01],&nbsp; &nbsp; &nbsp; &nbsp;[&nbsp; 9.60980000e+00,&nbsp; &nbsp;9.75000000e+01,&nbsp; &nbsp;9.60000000e+01,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1.03500000e+02,&nbsp; &nbsp;1.05000000e+02,&nbsp; &nbsp;5.00000000e-02],&nbsp; &nbsp; &nbsp; &nbsp;[&nbsp; 9.60981000e+00,&nbsp; &nbsp;9.75000000e+01,&nbsp; &nbsp;9.60000000e+01,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1.05000000e+02,&nbsp; &nbsp;1.06500000e+02,&nbsp; &nbsp;3.00000000e-02],&nbsp; &nbsp; &nbsp; &nbsp;[&nbsp; 9.60984000e+00,&nbsp; &nbsp;9.75000000e+01,&nbsp; &nbsp;9.60000000e+01,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1.09500000e+02,&nbsp; &nbsp;1.11000000e+02,&nbsp; &nbsp;1.00000000e-02]])>>> darray([[&nbsp; 9.60977000e+00,&nbsp; &nbsp;9.90000000e+01,&nbsp; &nbsp;1.00500000e+02,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 9.75000000e+01,&nbsp; &nbsp;9.60000000e+01,&nbsp; &nbsp;1.58000000e+00],&nbsp; &nbsp; &nbsp; &nbsp;[&nbsp; 9.60979000e+00,&nbsp; &nbsp;1.02000000e+02,&nbsp; &nbsp;1.03500000e+02,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 9.75000000e+01,&nbsp; &nbsp;9.60000000e+01,&nbsp; &nbsp;1.10000000e-01],&nbsp; &nbsp; &nbsp; &nbsp;[&nbsp; 9.60980000e+00,&nbsp; &nbsp;1.03500000e+02,&nbsp; &nbsp;1.05000000e+02,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 9.75000000e+01,&nbsp; &nbsp;9.60000000e+01,&nbsp; &nbsp;5.00000000e-02],&nbsp; &nbsp; &nbsp; &nbsp;[&nbsp; 9.60981000e+00,&nbsp; &nbsp;1.05000000e+02,&nbsp; &nbsp;1.06500000e+02,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 9.75000000e+01,&nbsp; &nbsp;9.60000000e+01,&nbsp; &nbsp;3.00000000e-02],&nbsp; &nbsp; &nbsp; &nbsp;[&nbsp; 9.60984000e+00,&nbsp; &nbsp;1.09500000e+02,&nbsp; &nbsp;1.11000000e+02,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 9.75000000e+01,&nbsp; &nbsp;9.60000000e+01,&nbsp; &nbsp;1.00000000e-02]])说明:&nbsp;>>> np.isin(a[:,0],b[:,0])array([ True, False,&nbsp; True,&nbsp; True,&nbsp; True, False,&nbsp; True], dtype=bool)上面的内容基本上只是向您显示的第一列的值a可以在的第一列中找到的位置,b然后可以a使用上面显示的代码通过该布尔数组进行索引:c = a[np.isin(a[:,0],b[:,0])]
随时随地看视频慕课网APP

相关分类

Python
我要回答