gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:22:56 26 4
gpt4 key购买 nike

我在 python 框架 A 中

enter image description here

和框架B:

enter image description here

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

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

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

enter image description here

非常感谢。此致吉安卡洛

最佳答案

你的合并失败是怎么回事?它应该适用于以 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

关于Python 用一个公共(public)列连接两个框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60280604/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com