← Back to Pandas Course | Chapter 8: Merging & Joining | Lesson 3 of 7

Left/Right/Outer Join करना

चुनें कि unmatched rows को कैसे treat किया जाए: सभी left rows रखें, सभी right rows रखें, या सब कुछ।

In this page:

  1. Left/right/outer join
Syntax
python
pd.merge(left, right, on="key_column", how="left")
pd.merge(left, right, on="key_column", how="outer")

Left/right/outer join

how="left" left table की हर row रखता है, how="right" हर right row रखता है, और how="outer" दोनों की सभी rows रखता है। बिना partner वाली rows को NaN से fill किया जाता है। indicator=True option एक column जोड़ता है जो दिखाता है कि हर row कहां से आई।

Note: joins को audit करने के लिए indicator=True बहुत अच्छा है।

उदाहरण: Left/right/outer join

python
import pandas as pd

emp = pd.DataFrame({"name": ["Ann", "Bob", "Cy"], "dept_id": [10, 20, 30]})
dept = pd.DataFrame({"dept_id": [10, 20, 40], "dept": ["Sales", "Tech", "HR"]})
print(emp.merge(dept, on="dept_id", how="left"))
print(emp.merge(dept, on="dept_id", how="outer", indicator=True))

# Output:
#   name  dept_id   dept
# 0  Ann       10  Sales
# 1  Bob       20   Tech
# 2   Cy       30    NaN
#   name  dept_id   dept      _merge
# 0  Ann       10  Sales        both
# 1  Bob       20   Tech        both
# 2   Cy       30    NaN   left_only
# 3  NaN       40     HR  right_only
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. left versus right के लिए गलत पक्ष चुनना
  2. unmatched rows से बने NaN को नज़रअंदाज़ करना
  3. यह भूल जाना कि outer joins result को बढ़ा सकते हैं
चैप्टर सारांश
  • left सभी left rows रखता है
  • right सभी right rows रखता है
  • outer सभी rows रखता है
  • indicator source दिखाता है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.