gpt4 book ai didi

python - Pandas 连接 2 个数据框

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

我有 2 个数据框:

>>> result
id order_nr
0 3131334 334756912
0 3131312 386517432
0 3131309 356299432
0 3131205 397514312
...

>>> OMSdf
order_nr status refund_amount
0 377766482 already_canceled 0
1 348722582 already_canceled 0
2 395287472 on_hold 0
3 345883652 already_canceled 0
...

这 2 个数据帧在“order_nr”字段中有一些共同的值:

>>> result[result['order_nr'] == '377766482']
id order_nr
0 3129153 377766482

>>> OMSdf[OMSdf['order_nr'] == '377766482']
order_nr status refund_amount
0 377766482 already_canceled 0

但我尝试将它们结合在一起:

resultfinal = result.join(OMSdf, on='order_nr', how='inner', lsuffix='', rsuffix='_oms', sort=False)

结果为空:

>>> resultfinal
Empty DataFrame
Columns: [id, order_nr, order_nr_oms, status, refund_amount]
Index: []

最佳答案

使用下面的代码

df_merged = pd.merge(result, OMSdf, how='inner')

就您而言,您可以在加入之前将 order_nr 作为两个数据帧的索引,如下所示

result.index = result['order_nr']
OMSdf.index = OMSdf['order_nr']

关于python - Pandas 连接 2 个数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31503980/

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