I have 2 numpy arrays:
A
[['12345' 45 100]
['12345' 29 100]
['45451' 23 0]
['56789' 45 450]
['56789' 56 50]
['56567' 5 500]
['89321' 15 0]
['90234' 43 40]
['90234' 55 0]
['99843' 18 5500]]
B
[['12345']
['90234']
['45451']
['56789']
['89321']]
I need to leave in array B only rows where elements equals the elements from 1 column in array A and the 3rd column contains value "0". Basically, these are '45451' and '89321'.
I have the next code:
for ind1 in range(len(A)):
for ind2 in range(len(B)):
if (A[ind1][0] == B[ind2]) and (A[ind1][2] > 0):
B = np.delete(B, ind2, axis=0)
B
I've got the following error message:
IndexError: index 4 is out of bounds for axis 0 with size 4
I suppose it's something with array dimensions but can't figure out. Would appreciate if you can fix the code :)
Tried to change the conditions
CodePudding user response:
In this line
if (A[ind1][0] == B[ind2]) and (A[ind1][2] > 0):
It should be as
if (A[ind1][:, :1] == B[ind2]) and (A[ind1][2] > 0):
In the 2nd line where the changes made, you are trying to match the first column in array A to array B, this should work
CodePudding user response:
import numpy as np
a = np.array(
[[12345, 45, 100],
[12345, 29, 100],
[45451, 23, 0],
[56789, 45, 450],
[56789, 56, 50],
[56567, 5, 500],
[89321, 15, 0],
[90234, 43, 40],
[90234, 55, 0],
[99843, 18, 5500]])
b = np.array(
[[12345],
[90234],
[45451],
[56789],
[89321]])
print(*b[np.isin(b, a[a[:,2]==0][:,0])])