I have 2 dataframes
id nr lval
0 1 one 1
1 2 two 2
2 3 one 3
id nr rval
4 1 one 4
5 2 one 5
6 3 one 6
I need merge or combine these two dataframes and use column 'id' as a key. By collision (column 'nr') take the column from first dataframe. Result should looks like this:
id nr lval rval
0 1 one 1 4
1 2 two 2 5
2 3 one 3 6
As the shapes of the dfs are the same you can call combine_first
, the lhs df values supercede the rhs df hence you keep the nr column values:
In [3]:
df.combine_first(df1)
Out[3]:
id lval nr rval
0 1 1 one 4
1 2 2 two 5
2 3 3 one 6
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.