I have two dataframes:
A = pd.DataFrame(data=np.array([['t1',1,'t2',2]]).reshape(2,2),columns=['a','b'])
A
Out[6]:
a b
0 t1 1
1 t2 2
B = pd.DataFrame(data=np.array([[1,2,3],[2,5,6],[3,6,7]]).reshape(3,3),columns=['x','y','z'])
B
Out[8]:
x y z
0 1 2 3
1 2 5 6
2 3 6 7
I am trying to basically match columns 'x' of dataframe B on column 'b' dataframe A but replace the matched values with column 'a' of dataframe A.
ie I want to merge the two dataframes so that the output will look like this:
x y z
0 t1 2 3
1 t2 5 6
2 3 6 7
Any ideas how to go about this?
B.loc[B.x.astype(str).isin(A.b), 'x'] = A.a
B
x y z
0 t1 2 3
1 t2 5 6
2 3 6 7
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.