python - Numpy Compare unequal rows and make both array of same dimension -
a= [[43655, 1428, 0, 2554] [44580, 1428, 0, 2555] [44930, 1428, 0, 2556] [47708, 1428, 0, 2557]] b= [[41641, 1428, 0, 2554] [44075, 1428, 0, 2555] [44901, 1428, 1, 2556] [45377, 1428, 0, 2557] [48056, 1428, 0, 2558]] new b= [[41641, 1428, 0, 2554] [44075, 1428, 0, 2555] [44901, 1428, 1, 2556] [45377, 1428, 0, 2557]
i have 2 numpy array unequal rows. eg. array a
has 4 rows while array b
has 5 rows.
edit: no. of rows in array 'b' greater array 'a'. every element of a[:,3] lies in b[:,3]. there function extract rows of array b b[:,3]=a[:,3]
you can compare elements of 3rd column using zip
, np.equal
within list comprehension convert result numpy array , desire rows array b
.
>>> b[np.array([np.equal(*i) in zip(a[:,3],b[:,3])])] array([[41641, 1428, 0, 2554], [44075, 1428, 0, 2555], [44901, 1428, 1, 2556], [45377, 1428, 0, 2557]])
if order not important you can use np.in1d
:
>>> b[np.in1d(b[:,3],a[:,3])] array([[41641, 1428, 0, 2554], [44075, 1428, 0, 2555], [44901, 1428, 1, 2556], [45377, 1428, 0, 2557]]) >>> a=np.array([[100, 1], [101, 4], [106, 6], [104, 10]]) >>> b= np.array([[ 1, 1], [ 2, 2], [ 3, 3], [ 4, 4], [ 5, 5], [ 6, 6], [ 7, 7], [ 8, 8], [ 9, 9], [10, 10]]) >>> >>> b[np.in1d(b[:,1],a[:,1])] array([[ 1, 1], [ 4, 4], [ 6, 6], [10, 10]])
Comments
Post a Comment