Left/Right/Outer Join करना
चुनें कि unmatched rows को कैसे treat किया जाए: सभी left rows रखें, सभी right rows रखें, या सब कुछ।
In this page:
Syntax
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
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. #}
आम गलतियां
- left versus right के लिए गलत पक्ष चुनना
- unmatched rows से बने NaN को नज़रअंदाज़ करना
- यह भूल जाना कि 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: