🔵 Data Science · Lesson 39
GroupBy, Merge और Pivot Tables
Pandas groupby और merge क्या है?
Pandas groupby और merge ka matlab hai: groupby summarizes data by category, and merge combines two DataFrames using a common column. Simple words me, ye topic practical Python programs likhne me direct use hota hai.
Is topic ko sirf definition ke liye nahi, balki class-wise summaries jaise real examples ke liye practice karein.
यह क्यों सीखना जरूरी है?
- Ye class-wise summaries me kaam aata hai.
- Ye combining related tables se bhi connected hai.
- Isse aap code ka output aur errors better samajh paate hain.
Important Terms
| Term | Meaning |
|---|---|
| groupby | Pandas operation used to summarize data by category. |
| aggregation | Summary calculation such as sum, average or count. |
| merge | Combining tables using a common column. |
| join | join is an important term in this topic. |
| summary | summary is an important term in this topic. |
Syntax / Basic Pattern
Basic idea: pehle data तैयार करें, phir Python logic apply करें, aur finally result display करें.
Basic Pattern
import pandas as pd
df = pd.DataFrame({
"Class": ["X", "X", "XI"],
"Marks": [80, 90, 85]
})
summary = df.groupby("Class")["Marks"].mean()
print(summary)Complete Example Program
Python – pandas-groupby-merge.py
import pandas as pd
df = pd.DataFrame({
"Class": ["X", "X", "XI"],
"Marks": [80, 90, 85]
})
summary = df.groupby("Class")["Marks"].mean()
print(summary)Expected Output
Class
X 85.0
XI 85.0
Name: Marks, dtype: float64
Program Explanation
import pandas as pdimports ready-made features from a module/library.df = pd.DataFrame({stores a value in df."Class": ["X", "X", "XI"],performs the next step of the program logic."Marks": [80, 90, 85]performs the next step of the program logic.})performs the next step of the program logic.summary = df.groupby("Class")["Marks"].mean()stores a value in summary.print(summary)displays information or calculated result on the screen.
Practical Uses
- Class-wise summaries.
- Combining related tables.
- Category-wise analysis.
Common Mistakes
- Analysing data before checking missing values, duplicates and data types.
- Changing original data without keeping a clean copy.
- Creating charts without title, labels or explanation.
Practice Tasks
- Program ko
pandas-groupby-merge.pyfile me type karke run karein. - Values change karke output compare karein.
- class-wise summaries par ek छोटा example banayen.
- Logic ko apne words me 5 lines me likhein.
सारांश
Pandas groupby and merge ko tab complete maanenge jab aap iska meaning, example, output aur practical use clearly explain kar saken.
💻 लाइव कोड एडिटर
इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.