🔵 Data Science · Lesson 39
GroupBy, Merge and Pivot Tables
What is Pandas groupby and merge?
Pandas groupby and merge means groupby summarizes data by category, and merge combines two DataFrames using a common column.
In real programs, this topic helps in class-wise summaries. Learn the idea first, then type the program yourself and compare the output.
💡 At a Glance
| Point | Details |
|---|---|
| Course Area | Data Science Tools and concepts used to analyse, clean and present data. |
| Main Use | class-wise summaries |
| Example File | pandas-groupby-merge.py |
| Practice Focus | Run, change values, and explain the output line by line. |
Why should you learn this?
- It is useful for class-wise summaries.
- It connects with combining related tables.
- It improves your ability to read, write and debug Python programs.
Important Terms
These terms are used directly in this lesson. Understand them before memorising the code.
| 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
The simple pattern is: prepare data, apply the concept, then show the result.
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.
Where will you use it?
- 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
- Type the program in
pandas-groupby-merge.pyand run it. - Change input values or sample data and observe the new output.
- Create one example related to class-wise summaries.
- Write 5 lines explaining the logic in your own words.
Summary
Pandas groupby and merge is not a theory-only topic. You should be able to explain the meaning, write the example, run it successfully, and use it in a small practical program.
💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.