chribsen
chribsen

Reputation: 6620

Python: Joining two dataframes on a primary key

I have two DataFrames A and B. I want to replace the rows in A with rows in B where a specific column is equal to each other.

A:
      1           2           3
0   asd     0.304012       0.358484
1   fdsa    -0.198157      0.616415
2   gfd    -0.054764       0.389018
3   ff         NaN         1.164172

B:
      1           2           3
0   asd     10.4012       1.458484
1   fdsa    100.198157      2.015

I want the following result:

      1           2           3
0   asd     10.4012        1.458484   (row merged from B on column 1)
1   fdsa    100.198157     2.015      (row merged from B on column 1)
2   gfd    -0.054764       0.389018
3   ff         NaN         1.164172

Upvotes: 0

Views: 1350

Answers (1)

EdChum
EdChum

Reputation: 394099

Just call update: this will overwrite the lhs df with the contents of the rhs df where there is a match in your case replace df and df1 with A and B respectively:

In [13]:

df.update(df1)
df
Out[13]:
      1           2         3
0   asd   10.401200  1.458484
1  fdsa  100.198157  2.015000
2   gfd   -0.054764  0.389018
3    ff         NaN  1.164172

Upvotes: 2

Related Questions