I have two dataframes:
df1:
A B C
0 1 3 5
1 2 4 6
df2:
A B C
0 1 3 50
1 3 5 -1
2 2 4 60
And now I want to update df1 from df2 based on the same values from A
and B
, to get something like this:
A B C
0 1 3 50
1 2 4 60
What did I try:
.update()
results in A[1] == 3
nad B[1] == 5
, it just goes in order, doesn't match the key (because I cannot specify it there) .merge()
with left join and on=["A", "B"]
- best I got so far, it does preserve the result df to just 2 rows with A=1,2
and B=3,4
, but it adds columns C_x
and C_y
, the latter with the values I want, but I want them to be in C
Is there a clean way to do this, or should I go for .merge()
and just remove the column with _x
suffix + rename the C_y
to C
?
Drop C in df1 then merge
df1.drop('C', axis=1).merge(df2, on=['A', 'B'], how='left')
A B C
0 1 3 50
1 2 4 60
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.