合并时join条件和case的组合

问题描述 投票:0回答:2
select a.year,a.country,b.amount
from table_a a
left join table_b b
on a.country=b.country
and (CASE WHEN b.country = 'Europe' THEN b.year = 2022 ELSE b.year = 2023 END)

我正在尝试将这段代码转换为Python。我已经尝试过使用

pd.merge()
进行左连接,但不确定在 pandas 合并的连接条件下如何继续使用 case。我该如何让它发挥作用?

python sql pandas join left-join
2个回答
0
投票

您不需要在合并中包含该条件,只需在完成后使用它来过滤结果即可。像这样的东西:

out = df_a.merge(df_b, on='country', how='left', suffixes=['','_b'])
mask = (out['country'] == 'Europe') & (out['year_b'] == 2022) | \
   (out['country'] != 'Europe') & (out['year_b'] == 2023)
out = out.loc[mask, ['year', 'country', 'amount']]

例如,使用以下示例数据:

df_a = pd.DataFrame({
    'country': ['Europe', 'USA', 'Africa'],
    'year': [2022, 2023, 2021]
})
df_b = pd.DataFrame({
    'country': ['Europe', 'USA', 'Africa', 'USA', 'Europe'],
    'year': [2023, 2022, 2022, 2023, 2022],
    'amount': [10, 20, 30, 40, 50]
})

输出将是:

   year country  amount
1  2022  Europe      50
3  2023     USA      40

0
投票

由于条件确实是用来过滤

table_b
之前的,所以我们可以先使用条件过滤
table_b
,然后再合并。

output = (
    table_a.merge(
        table_b.query("(country == 'Europe' and year == 2022) or (country != 'Europe' and year == 2023)"), 
        on=['country', 'year'], how='left')
    .filter(['year', 'country', 'amount'])
)

使用 Nick 的示例,上面的代码会产生以下输出:

  country  year  amount
0  Europe  2022    50.0
1     USA  2023    40.0
2  Africa  2021     NaN
© www.soinside.com 2019 - 2024. All rights reserved.