Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:在主键上连接两个数据帧_Python_Pandas - Fatal编程技术网

Python:在主键上连接两个数据帧

Python:在主键上连接两个数据帧,python,pandas,Python,Pandas,我有两个数据帧A和B。我想用B中的行替换A中的行,其中特定列彼此相等 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

我有两个数据帧A和B。我想用B中的行替换A中的行,其中特定列彼此相等

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
我希望得到以下结果:

      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
只需调用:这将使用rhs df的内容覆盖lhs df,其中在您的案例中存在匹配项,分别将
df
df1
替换为
a
B

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

你可以发布代码和数据吗?你所问的是非常通用的,但是你的数据模式可能有一些特定的要求,也显示了所需的输出。我现在已经做了一个例子。谢谢你的提醒。