Python用一个公共列连接两个框架

时间:2020-02-18 12:09:32

标签: python pandas

我在python框架A中有

enter image description here

和框架B:

enter image description here

如何在框架A中添加新列“名称”,以显示框架b中的z列值?两个框架之间的公共列是A['b']B['v']

我正在尝试使用pandas concat或merge,但是我失败了。

我在A帧中得到的预期结果是:

enter image description here

非常感谢。 最好的祝福 吉安卡洛

1 个答案:

答案 0 :(得分:1)

您的合并失败了吗?它应与左侧的A一起left连接,并指定left_onright_on列:

final_output = A.merge(B,how='left',left_on='b',right_on='v').rename(columns={'z':'name'}).drop(columns='v')

输出:

     a  b    c     d   name
0  Yes  1  Yes   Buy  name1
1  Yes  2  Yes  Sell  name2
2  Yes  3  Yes   Buy  name3
3  Yes  4  Yes  Sell  name4