Reputation: 220
How can I join two numpy arrays together, based on common values in each first column, or zero if no match? The arrays need not be the same length. The first column in each array represents a unique ID, and the second column is the count, both obtained from np.unique. The ultimate goal is to simply subtract the counts of each unique ID.
a = np.array([[1,2],
[3,4]])
b = np.array([[1,10],
[2,20],
[3,30]])
Desired output:
c = np.array([[1,10,2],
[2,20,0],
[3,30,4]])
Upvotes: 0
Views: 2243
Reputation: 288
if all consecutive rows in b are present:
z = np.zeros((b.shape[0],1),dtype=int)
c = np.hstack((b,z))
ai = a[:, 0] - 1
c[ai,2] = a[:, 1]
print c
A more general solution, if both a and b have missing rows:
d = np.union1d(a[:, 0],b[:, 0]).reshape(-1,1)
z = np.zeros((d.shape[0],2),dtype=int)
c = np.hstack((d,z))
mask = np.in1d(c[:, 0], b[:, 0])
c[mask,1] = b[:, 1]
mask = np.in1d(c[:, 0], a[:, 0])
c[mask,2] = a[:, 1]
print c
Upvotes: 2