You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Pandas中实现类VLOOKUP功能:关联匹配币种对应USD金额

解决方法

两张表的共同键就是ccy列,直接用Pandas的merge方法就能完成需求,具体步骤如下:

1. 构造示例数据(方便复现)

import pandas as pd

# 创建table1
table1 = pd.DataFrame({
    'transaction': ['First', 'Second', 'Third'],
    'traded': [-5, 21, 14],
    'ccy': ['AUD', 'USD', 'USD']
})

# 创建table2
table2 = pd.DataFrame({
    'counter': [0, 1, 2],
    'ccy': ['USD', 'ARS', 'AUD'],
    'USD Amount': [1, 0.01, 0.83]
})

2. 关联两张表并新增列

因为只需要table2里的ccy和USD Amount字段,先筛选出这两列,再用左连接(how='left')保证table1的所有行都被保留:

# 关联后得到带USD Amount的新表
table1_with_usd = pd.merge(
    table1,
    table2[['ccy', 'USD Amount']],  # 仅保留需要的关联列
    on='ccy',  # 指定共同关联键为ccy
    how='left'
)

3. 最终结果

运行后table1_with_usd的内容如下:

transactiontradedccyUSD Amount
First-5AUD0.83
Second21USD1.00
Third14USD1.00

内容的提问来源于stack exchange,提问作者MBlrd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 03:04:56