M6-006Mediumpandas 管道 · pandas pipelines🔥 大厂高频出现 1 次

merge 之后行数从 100 万变成 300 万,怎么排查?

After a merge, row count goes from one million to three million. How do you debug it?
💡 考场高分一句话答案:

右表关联键重复导致笛卡尔膨胀;先检查键唯一性,再决定去重或用 validate 参数断言。

The right-hand key is not unique, so the join fans out. Check key uniqueness, then dedup or aggregate the right table, and use validate to make it fail loudly.

🧭 Playbook 核心提示 / Actionable Calibration:

加 assert len(merged) == len(left) 是最便宜的自检,面试里写出来会显得有工程习惯。

📌 核心考察要点 (Key Points)

  • 诊断:df_r.groupby(key).size().max() 看是否 >1
  • 预防:merge(..., validate='many_to_one') 会直接报错
  • 指标影响:膨胀会让求和类指标翻倍,比率类可能不变——所以只查比率会漏掉
  • 处理:右表先去重(按业务规则取最新),或用聚合后的右表
  • Diagnose: right.groupby(key).size().max() above 1 confirms duplication
  • Prevent: merge(..., validate='many_to_one') raises instead of silently expanding
  • Metric impact: sums double while ratios may not move, so checking only ratios hides the bug
  • Fix: dedup the right table by business rule, or pre-aggregate it

🔍 大厂面试官连环追问 (Follow-ups)

  • many_to_many 什么时候是合理的?
  • merge 后 NaN 变多说明什么?

🎯 关联大厂考题实战训练 (1 题)

嫌字多看太累?进入苏格拉底式对话推演实验室

零打字负担,以大厂考官真实困境切入,通过点选直觉选项暴露思维盲区,手把手推导 Why。

💬 开启对话式破冰 ➔
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔