I have a numpy array and I would like to update a column of values in it with data from a second array. Somewhat like a vlookup function in excel.
Need to look-up the first column of a in the b table. Then replace the second column in a with the number from the second column in b.
import numpy as np
# type, newval
a = np.array( [[1, 23, 0],
[2, 24, 0],
[1, 15, 0],
[1, 27, 0],
[6, 22, 0],
[1, 18, 0]]
)
# type, newval
b = np.array([[1, 1.1],
[2, 2.1],
[3, 3.1],
[4, 4.1],
[5, 5.1],
[6, 6.1]]
)
a[:,2] = np.where(b[:,0] == a[:,0], b[:,1], None)
Expected result
Note: I would like the original array a to be updated with the lookup values.
a = array( [[1, 23, 1.1],
[2, 24, 2.1],
[1, 15, 1.1],
[1, 27, 1.1],
[6, 22, 6.1],
[1, 18, 1.1]]
)
What I get however is nan beside the last 4 items in the array. It likes like my np.where condition is replacing the value where the position AND the number are correct, not just where the number matches.
barray can be a list or any other type of object if it makes things easier. Theaarray is read in from a file so I'd prefer not to change the structure of that.