Merge, Join, Concatenate in Pandas in hindi
Merge, Join, Concatenate in Pandas – Complete Guide in Hindi
Table of Contents: Merge, Join, Concatenate in Pandas Explained in Hindi
Merge, Join, Concatenate in Pandas in Hindi
आज के समय में Data Analysis और Data Science में Pandas सबसे ज़्यादा इस्तेमाल की जाने वाली Python library है। College exams, competitive exams और practical projects — हर जगह Pandas से related questions पूछे जाते हैं। इस article में हम Merge, Join, और Concatenate in Pandas को बिल्कुल classroom style में, step-by-step और easy Hindi language में समझेंगे।
यह content specially exam-oriented है, ताकि आपको theory के साथ-साथ practical understanding भी मिल सके। हर concept को simple examples, clear explanation और proper structure में cover किया गया है।
Merge in Pandas
Merge in Pandas का use तब किया जाता है जब हमें दो अलग-अलग DataFrame को किसी common column के basis पर combine करना हो। यह concept बिल्कुल SQL के JOIN जैसा होता है, इसलिए students को इसे समझना थोड़ा आसान लगता है।
जब किसी table में Student ID हो और दूसरी table में उसी Student ID के साथ Marks हों, तो उन दोनों tables को एक साथ लाने के लिए हम merge() function का use करते हैं।
Why Merge is Important
Real-world data हमेशा एक ही table में नहीं मिलता। Data अलग-अलग sources में होता है और analysis से पहले उसे combine करना ज़रूरी होता है। यही काम Pandas merge बहुत efficiently करता है।
- Multiple DataFrame को combine करने के लिए
- Common column के basis पर data match करने के लिए
- Relational data handle करने के लिए
Syntax of Merge in Pandas
Pandas में merge करने के लिए pd.merge() function का use किया जाता है।
pd.merge(dataframe1, dataframe2, on='column_name', how='type')
यहाँ पर on का मतलब है common column और how से यह decide होता है कि merge किस type का होगा।
Types of Merge in Pandas
Pandas में mainly चार types के merge होते हैं, जो exam point of view से बहुत important हैं।
| Merge Type | Explanation |
|---|---|
| Inner Merge | केवल matching rows को merge करता है |
| Left Merge | Left DataFrame की सारी rows रखता है |
| Right Merge | Right DataFrame की सारी rows रखता है |
| Outer Merge | दोनों DataFrame की सारी rows रखता है |
Exam में अक्सर पूछा जाता है कि inner merge और outer merge में क्या difference है, इसलिए इस table को अच्छे से याद रखना बहुत ज़रूरी है।
Simple Example of Merge
मान लो हमारे पास दो DataFrame हैं — एक में Student details और दूसरे में Marks।
pd.merge(students, marks, on='roll_no', how='inner')
इस example में केवल वही students आएँगे जिनका roll_no दोनों DataFrame में मौजूद है। यह concept college exams में short notes और numericals में बहुत काम आता है।
Join in Pandas
Join in Pandas भी DataFrame को combine करने के लिए use किया जाता है, लेकिन इसका working merge से थोड़ा अलग होता है। Join mainly index के basis पर data को combine करता है।
अगर आपके DataFrame में index already set है, तो join एक faster और cleaner option बन जाता है। इसी वजह से large datasets में join को prefer किया जाता है।
Difference Between Merge and Join
Students अक्सर merge और join में confused हो जाते हैं, इसलिए इनके basic difference को clear करना बहुत ज़रूरी है।
- Merge column-based होता है
- Join index-based होता है
- Merge ज्यादा flexible है
- Join faster execution देता है
Syntax of Join in Pandas
Pandas में join करने के लिए join() method का use किया जाता है।
dataframe1.join(dataframe2, how='type')
यहाँ भी how parameter वही काम करता है जो merge में करता है। Left, right, inner और outer — सभी join में available होते हैं।
Example of Join
अगर Student DataFrame का index roll number है, तो marks DataFrame को directly join किया जा सकता है।
students.join(marks, how='left')
इस example में students DataFrame की सारी rows रहेंगी और marks जहाँ available होंगे वहीं add हो जाएँगे।
यह concept exam में long answer questions के लिए बहुत useful होता है।
Concatenate in Pandas
Concatenate in Pandas का मतलब होता है multiple DataFrame या Series को एक साथ जोड़ना। यह process बिल्कुल simple होता है और तब use किया जाता है जब data को side-by-side या top-to-bottom जोड़ना हो। College exams में यह topic अक्सर short notes और difference-based questions में आता है।
अगर आपके पास same structure वाले multiple DataFrame हैं और आप उन्हें एक single DataFrame बनाना चाहते हैं, तो pd.concat() सबसे best option होता है।
Why Concatenate is Used
Real-world में data अलग-अलग files या parts में होता है। Analysis से पहले उस data को combine करना ज़रूरी होता है। Concatenate इसी problem को solve करता है।
- Multiple DataFrame को merge किए बिना जोड़ने के लिए
- Same columns वाले data को combine करने के लिए
- Fast data stacking के लिए
Syntax of Concatenate in Pandas
Pandas में concatenate करने के लिए pd.concat() function का use किया जाता है।
pd.concat([dataframe1, dataframe2], axis=0)
यहाँ axis parameter decide करता है कि data vertical जुड़ेगा या horizontal। Axis का concept exam point of view से बहुत important होता है।
Understanding Axis in Concatenate
Axis का मतलब होता है direction। Pandas में mainly दो axis होते हैं — axis=0 और axis=1।
| Axis | Meaning |
|---|---|
| axis = 0 | Rows के form में data जुड़ता है |
| axis = 1 | Columns के form में data जुड़ता है |
Exam में कई बार पूछा जाता है कि axis=0 default क्यों होता है। Answer यह है कि by default Pandas rows को stack करता है।
Vertical Concatenation Example
जब दो DataFrame में same columns होते हैं और हम rows add करना चाहते हैं, तो vertical concatenation किया जाता है।
pd.concat([df1, df2], axis=0)
इस case में df2 की rows df1 के नीचे add हो जाती हैं। यह technique monthly data, yearly reports और logs analysis में बहुत useful होती है।
Horizontal Concatenation Example
अगर हमें columns को side-by-side जोड़ना हो, तो axis=1 use किया जाता है।
pd.concat([df1, df2], axis=1)
इसमें df1 और df2 के columns एक ही row index के साथ combine होते हैं। यह तब काम आता है जब index same हो।
Handling Index in Concatenate
Concatenate करते समय index duplicate हो सकता है। इस problem को solve करने के लिए Pandas special option देता है।
pd.concat([df1, df2], ignore_index=True)
ignore_index=True करने से Pandas automatically नया index generate कर देता है। Exam में यह parameter direct question के रूप में पूछा जा सकता है।
Merge vs Join vs Concatenate
Students को अक्सर confusion होता है कि किस situation में कौन सा method use करें। इसलिए clear comparison समझना बहुत ज़रूरी है।
| Feature | Merge | Join | Concatenate |
|---|---|---|---|
| Basis | Column | Index | Axis |
| Flexibility | High | Medium | Low |
| Complex Data | Yes | Yes | No |
| Exam Usage | Very High | High | Medium |
अगर question relational data का है, तो merge best रहता है। अगर index already clean है, तो join use करो। Simple stacking के लिए concatenate सबसे easy है।
Common Mistakes Students Make
Exams और practicals में students कुछ common mistakes करते हैं, जो marks loss का reason बनती हैं।
- Merge और Concatenate को same समझ लेना
- Axis concept को ignore करना
- Index mismatch को ध्यान न देना
- How parameter को गलत use करना
अगर आप इन mistakes से बचते हो, तो Pandas के questions बहुत आसानी से solve हो जाते हैं।
Exam-Oriented Tips
College exams में Pandas के questions mostly theory + output based होते हैं। इसलिए concept clarity सबसे important होती है।
- Merge types के differences याद रखो
- Join हमेशा index से related होता है
- Concatenate axis-based होता है
- Syntax simple और clean लिखो
अगर आप इन points को ध्यान में रखकर पढ़ते हो, तो Merge, Join, Concatenate in Pandas आपके लिए scoring topic बन सकता है।