Issue
I have two dataframe:
the first one, let's say dfrA
x,y,z
0,0,1
0,1,2
0,2,3
0,3,4
1,0,5
1,1,6
1,2,7
1,3,8
2,0,9
2,1,10
2,2,11
2,3,12
3,0,13
3,1,14
3,2,15
3,3,16
and another one, let's say dfrB
x,y
1,2
2,3
I would like to add a column in dfrB according with z value in the dfrA which has the same x and y of the dfrB.
In other words I expect:
x,y,z
1,2,7
2,3,12
I am able to a empty column to dfrB:
df_support = pd.DataFrame(columns=['z'])
dfrB = dfrB.join(df_support, how="outer")
how can now fill column z in dfrB? I would like to avoid to do a cycle full of if.
Solution
You can try pandas.DataFrame.merge
dfrB['z'] = dfrB.merge(dfrA, on=['x', 'y'], how='left')['z']
print(dfrB)
x y z
0 1 2 7
1 2 3 12
Answered By - Ynjxsjmh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.