Pivot & Melt in Pandas in hindi
Pivot & Melt in Pandas – Complete Guide for Data Analysis (Hindi)
Table of Contents – Pivot & Melt in Pandas (Hindi)
Pivot & Melt in Pandas in Hindi
Data Analysis में Pandas library का बहुत बड़ा role होता है, खासकर जब हमें raw data को meaningful format में बदलना होता है। College exams, practicals और viva में अक्सर पूछा जाता है कि Pivot in Pandas क्या होता है और इसका use कब किया जाता है। इस part में हम केवल Pivot in Pandas in Hindi को detail और easy language में समझेंगे।
Pivot in Pandas in Hindi
Pivot in Pandas एक ऐसा technique है जिसकी मदद से हम table के data को rotate या reshape कर सकते हैं। इसका मतलब यह है कि rows को columns में और columns को rows में बदला जा सकता है। जब data बहुत बड़ा हो और हमें summary निकालनी हो, तब Pivot बहुत useful होता है।
Simple शब्दों में, Pivot हमें यह decide करने की power देता है कि कौन सा column index बने, कौन सा column actual column बने और किस value पर calculation हो। यही कारण है कि Pivot को data summarization का strong tool माना जाता है।
Why Pivot is Used in Pandas
Real-world data अक्सर long format में होता है, जिसे समझना मुश्किल हो जाता है। Pivot की मदद से हम उसी data को readable और exam-friendly format में बदल सकते हैं। यही format reports, charts और dashboards में use होता है।
- Large dataset को summarize करने के लिए
- Category-wise comparison के लिए
- Exam में table based questions solve करने के लिए
- Business और statistical reports बनाने के लिए
Pivot Syntax in Pandas in Hindi
Pandas में Pivot function बहुत simple syntax follow करता है। Syntax को समझना exam point of view से बहुत जरूरी है क्योंकि direct question पूछा जाता है।
Basic Pivot syntax इस प्रकार होता है:
DataFrame.pivot(index='column_name', columns='column_name', values='column_name')
यहाँ index वो column होता है जो row बनता है, columns वो column होता है जो new columns बनाता है, और values वो data होता है जिस पर table बनती है।
| Parameter | Meaning in Hindi |
|---|---|
| index | Row के रूप में use होने वाला column |
| columns | Column headings बनाने वाला column |
| values | Actual data values जो table में दिखेंगी |
Pivot Example in Pandas in Hindi
अब हम Pivot को एक simple example से समझते हैं ताकि concept clear हो जाए। मान लीजिए हमारे पास students का marks data है जिसमें subject-wise marks दिए गए हैं।
import pandas as pd
data = {
'Student': ['A', 'A', 'B', 'B'],
'Subject': ['Math', 'Science', 'Math', 'Science'],
'Marks': [80, 75, 85, 90]
}
df = pd.DataFrame(data)
यह data अभी long format में है। अब अगर हमें हर student के subject-wise marks एक ही row में देखने हों, तो हम Pivot का use करेंगे।
df.pivot(index='Student', columns='Subject', values='Marks')
Output में Student rows बनेंगे और Subject columns बन जाएंगे। इससे data बहुत साफ और readable दिखने लगता है। Exam में ऐसे output based questions common होते हैं।
Important Points About Pivot
Pivot function तभी work करता है जब index और columns का combination unique हो। अगर duplicate values होंगी तो error आ सकता है। यही point theory questions में पूछा जाता है।
- Pivot duplicate values handle नहीं करता
- Simple reshaping के लिए best method है
- Fast execution देता है
- College practicals में frequently used
अगर data में duplicate values हों, तो Pivot की जगह Pivot Table use किया जाता है, जिसमें aggregation possible होती है। Pivot और Pivot Table का difference अक्सर exams में पूछा जाता है।
Where Pivot is Asked in Exams
Skill, BSc Data Science, MCA और MBA analytics जैसे courses में Pivot topic बहुत important होता है। Practical exam में directly dataset दिया जाता है और Pivot apply करने को कहा जाता है।
Theory paper में definition, syntax और example-based questions common हैं। इसलिए Pivot को conceptually और practically दोनों तरह से समझना जरूरी है।
Melt in Pandas in Hindi
अब तक आपने Pivot in Pandas in Hindi को detail में समझ लिया है। अब हम उसी का opposite concept समझेंगे, जिसे Pandas में Melt कहा जाता है। College exams में Pivot के साथ-साथ Melt भी बहुत बार पूछा जाता है।
Melt in Pandas का use wide format data को long format में convert करने के लिए किया जाता है। यानी जहाँ Pivot rows को columns बनाता है, वहीं Melt columns को rows में बदल देता है। यह concept समझने में आसान है लेकिन practical में बहुत powerful है।
Why Melt is Used in Pandas
कई बार data analysis में हमें ऐसा data चाहिए होता है जो single column में values दिखाए। Visualization tools, statistical analysis और machine learning models long format data को prefer करते हैं। ऐसे cases में Melt सबसे सही option होता है।
- Wide table को long table में convert करने के लिए
- Data visualization के लिए data prepare करने में
- Machine Learning preprocessing में
- Exam practicals में data reshaping के लिए
Melt Syntax in Pandas in Hindi
Melt function का syntax भी काफी simple होता है और exam point of view से बहुत important है। कई बार direct पूछा जाता है कि Melt function का syntax लिखिए।
Basic Melt syntax इस प्रकार होता है:
pandas.melt(frame, id_vars=None, value_vars=None, var_name=None, value_name='value')
यहाँ id_vars वो columns होते हैं जिन्हें same रखा जाता है, value_vars वो columns होते हैं जिन्हें melt किया जाता है। बाकी parameters output को readable बनाने में help करते हैं।
| Parameter | Meaning in Hindi |
|---|---|
| frame | DataFrame जिस पर Melt apply करना है |
| id_vars | Identifier columns जो repeat होंगे |
| value_vars | Columns जिन्हें rows में convert करना है |
| var_name | New column का नाम |
| value_name | Values column का नाम |
Melt Example in Pandas in Hindi
अब हम Melt को एक simple example से समझते हैं ताकि concept practically clear हो जाए। मान लीजिए हमारे पास students के subject-wise marks wide format में हैं।
import pandas as pd
data = {
'Student': ['A', 'B'],
'Math': [80, 85],
'Science': [75, 90]
}
df = pd.DataFrame(data)
इस table में subjects columns के रूप में हैं। अब अगर हमें subject और marks को single column format में लाना हो, तो हम Melt का use करेंगे।
pd.melt(df, id_vars=['Student'], var_name='Subject', value_name='Marks')
Output में हर row में एक subject और उसका marks आएगा। यही long format data कहलाता है। Exam में ऐसे output based questions बहुत common होते हैं।
Important Points About Melt
Melt function data को repeat करता है इसलिए output rows की संख्या बढ़ जाती है। यह normal behavior है और students को इसमें confusion नहीं होना चाहिए।
- Melt wide data को long data बनाता है
- Duplicate rows create होना normal है
- Visualization tools के लिए best format देता है
- Pivot का opposite concept है
Pivot vs Melt in Pandas in Hindi
Pivot और Melt दोनों Pandas के reshaping tools हैं, लेकिन दोनों का purpose एक-दूसरे से बिल्कुल उल्टा होता है। Exams में इनका difference short answer या table form में पूछा जाता है।
| Pivot | Melt |
|---|---|
| Long data को wide data में convert करता है | Wide data को long data में convert करता है |
| Rows से columns बनते हैं | Columns से rows बनती हैं |
| Summary और reports के लिए useful | Analysis और visualization के लिए useful |
| Duplicate values allow नहीं करता | Duplicate rows create कर सकता है |
Pivot & Melt in College Exams
Skill, MCA, BSc IT, Data Science और MBA Analytics जैसे courses में Pivot & Melt in Pandas in Hindi बहुत scoring topic माना जाता है। Theory और practical दोनों में यह topic almost हर syllabus में शामिल होता है।
अगर student Pivot और Melt दोनों को concept और example के साथ समझ ले, तो Pandas का data reshaping part पूरी तरह strong हो जाता है। यही reason है कि यह topic exams के लिए बहुत important है।