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

Pandas中Merge操作返回空DataFrame问题求助

Fixing Empty DataFrame After Pandas Merge

Hey there, let's break down why your merge is returning an empty (0, 4) DataFrame and fix it right away!

The Root Cause

Your merge code has a conflicting parameter combination: you're using both right_on='Loan_ID2' and right_index=True.

When you set right_index=True, pandas ignores the right_on argument and instead tries to match data1['Loan_ID'] with the index of data2. Looking at your data, data2's index is numeric values (1, 2, 3, 4, 5) while data1['Loan_ID'] is string values like LP001003—these don't overlap at all, so no rows get matched.

The Fix

Simply remove the right_index=True parameter. You already told pandas to use data2['Loan_ID2'] to match data1['Loan_ID'] with right_on='Loan_ID2', so that's all you need.

Here's the corrected code:

data_merged = data1.merge(right=data2, how='inner', left_on='Loan_ID', right_on='Loan_ID2', sort=False)

What to Expect

After running this, data_merged.shape should return (5, 4)—all 5 rows from both datasets will be matched correctly, since every Loan_ID in data1 has a corresponding Loan_ID2 in data2.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:03:05