2
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 two numpy array with unequal rows. For eg. Array a has 4 rows while array b has 5 rows.
Edit: No. of rows in array 'b' is greater than array 'a'. Every element of a[:,3] lies in b[:,3]. Is there any function that extract only the rows of array b whose b[:,3]=a[:,3]

2
  • Seems that the output is not what you have want? and how you want to compare the columns and reduce the b based on what logic? Commented Jul 22, 2015 at 15:45
  • Every element in a[:,3] is a subset of b[:,3]. So I was thinking to compare a[:,3] with b[:,3] and extract index of b[:,3] where it matches with a[:,3]. In above eg. index would be i=[0,1,2,3]. Commented Jul 22, 2015 at 22:53

3 Answers 3

1

You can compare your the elements of 3rd column using zip and np.equal within a list comprehension then convert the result to a numpy array and get the desire rows from array b.

>>> b[np.array([np.equal(*I) for 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 the order is not important for you 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]])
Sign up to request clarification or add additional context in comments.

2 Comments

>>> a array([[100, 1], [101, 4], [106, 6], [104, 10]]) >>> b array([[ 1, 1], [ 2, 2], [ 3, 3], [ 4, 4], [ 5, 5], [ 6, 6], [ 7, 7], [ 8, 8], [ 9, 9], [10, 10]]) is not showing the same behavior. New b should be=b array([[ 1, 1], [ 4, 4], [ 6, 6], [10, 10]])
@HaWa So the order is not important for you! let me update
1

You can omit the last element of an array by doing a[:-1]

Therefore you can omit the last row of an array by doing;

a[:,3] 
b[:-1,3]

Comments

0

if you don't know how much longer b is than a then you can just use

b[:a.shape[0]]

This will grab enough rows of b to compare to a.

So to grab a particular column like in your example, it would be

b[:a.shape[0], 3]

This technique will even work in the other direction (if b is smaller than a).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.