Introduction
Grouping and aggregation are among the most powerful features of Pandas for data analysis. The groupby() function allows you to divide data into groups based on one or more columns and then perform calculations such as sum, average, count, minimum, maximum, and more. These operations are widely used in business reports, sales analysis, finance, HR analytics, and data science projects. In this chapter, you’ll learn how to use GroupBy and aggregation functions through practical examples. Pandas GroupBy and Aggregation practice questions with solutions help to understand the concepts.
1. Python Program to Group Data by a Single Column
Problem Statement
Write a Python program to group students by their Department.
Python Solution
import pandas as pd
data = {
"Name": ["Rahul", "Aman", "Priya", "Sneha"],
"Department": ["IT", "HR", "IT", "HR"],
"Marks": [85, 90, 78, 88]
}
df = pd.DataFrame(data)
grouped = df.groupby("Department")
for department, records in grouped:
print(department)
print(records)
Sample Output
HR
Name Department Marks
1 Aman HR 90
3 Sneha HR 88
IT
Name Department Marks
0 Rahul IT 85
2 Priya IT 78
Explanation
The groupby() function divides the DataFrame into groups based on the values in the Department column.
Concepts Covered
groupby()- Data Grouping
- Group Iteration
2. Python Program to Calculate the Sum of Each Group
Problem Statement
Write a Python program to calculate the total marks for each department.
Python Solution
import pandas as pd
data = {
"Department": ["IT", "HR", "IT", "HR"],
"Marks": [85, 90, 78, 88]
}
df = pd.DataFrame(data)
result = df.groupby("Department")["Marks"].sum()
print(result)
Sample Output
Department
HR 178
IT 163
Name: Marks, dtype: int64
Explanation
The sum() function adds all values within each group.
Concepts Covered
groupby()sum()- Aggregation
3. Python Program to Calculate the Average of Each Group
Problem Statement
Write a Python program to calculate the average marks for each department.
Python Solution
import pandas as pd
data = {
"Department": ["IT", "HR", "IT", "HR"],
"Marks": [85, 90, 78, 88]
}
df = pd.DataFrame(data)
result = df.groupby("Department")["Marks"].mean()
print(result)
Sample Output
Department
HR 89.0
IT 81.5
Name: Marks, dtype: float64
Explanation
The mean() function calculates the average value for each group.
Concepts Covered
groupby()mean()- Average Calculation
4. Python Program to Count Records in Each Group
Problem Statement
Write a Python program to count the number of students in each department.
Python Solution
import pandas as pd
data = {
"Department": ["IT", "HR", "IT", "HR", "IT"]
}
df = pd.DataFrame(data)
result = df.groupby("Department").size()
print(result)
Sample Output
Department
HR 2
IT 3
dtype: int64
Explanation
The size() function returns the total number of rows in each group.
Concepts Covered
groupby()size()- Record Count
5. Python Program to Find the Maximum Value in Each Group
Problem Statement
Write a Python program to find the highest marks obtained in each department.
Python Solution
import pandas as pd
data = {
"Department": ["IT", "HR", "IT", "HR"],
"Marks": [85, 90, 78, 88]
}
df = pd.DataFrame(data)
result = df.groupby("Department")["Marks"].max()
print(result)
Sample Output
Department
HR 90
IT 85
Name: Marks, dtype: int64
Explanation
The max() function returns the highest value within each group.
Concepts Covered
groupby()max()- Maximum Value
6. Python Program to Find the Minimum Value in Each Group
Problem Statement
Write a Python program to find the lowest marks obtained in each department.
Python Solution
import pandas as pd
data = {
"Department": ["IT", "HR", "IT", "HR"],
"Marks": [85, 90, 78, 88]
}
df = pd.DataFrame(data)
result = df.groupby("Department")["Marks"].min()
print(result)
Sample Output
Department
HR 88
IT 78
Name: Marks, dtype: int64
Explanation
The min() function returns the smallest value in each group.
Concepts Covered
groupby()min()- Minimum Value
7. Python Program to Calculate Multiple Aggregations
Problem Statement
Write a Python program to calculate the sum, average, and maximum marks for each department.
Python Solution
import pandas as pd
data = {
"Department": ["IT", "HR", "IT", "HR"],
"Marks": [85, 90, 78, 88]
}
df = pd.DataFrame(data)
result = df.groupby("Department")["Marks"].agg(
["sum", "mean", "max"]
)
print(result)
Sample Output
sum mean max
Department
HR 178 89.0 90
IT 163 81.5 85
Explanation
The agg() function allows multiple aggregation operations to be performed simultaneously on grouped data.
Concepts Covered
groupby()agg()- Multiple Aggregations
8. Python Program to Group by Multiple Columns
Problem Statement
Write a Python program to group data by Department and Gender.
Python Solution
import pandas as pd
data = {
"Department": [
"IT",
"HR",
"IT",
"HR",
"IT"
],
"Gender": [
"Male",
"Female",
"Female",
"Female",
"Male"
],
"Marks": [85, 90, 78, 88, 95]
}
df = pd.DataFrame(data)
result = df.groupby(
["Department", "Gender"]
)["Marks"].mean()
print(result)
Sample Output
Department Gender
HR Female 89.0
IT Female 78.0
Male 90.0
Name: Marks, dtype: float64
Explanation
The groupby() function accepts multiple columns, creating hierarchical groups for more detailed analysis.
Concepts Covered
- Multiple Grouping
groupby()- Hierarchical Index
9. Python Program to Reset Index After Grouping
Problem Statement
Write a Python program to reset the index after performing a GroupBy operation.
Python Solution
import pandas as pd
data = {
"Department": ["IT", "HR", "IT", "HR"],
"Marks": [85, 90, 78, 88]
}
df = pd.DataFrame(data)
result = (
df.groupby("Department")["Marks"]
.mean()
.reset_index()
)
print(result)
Sample Output
Department Marks
0 HR 89.0
1 IT 81.5
Explanation
The reset_index() function converts the grouped index back into a regular column, making the result easier to read and export.
Concepts Covered
reset_index()- GroupBy Result
- Data Formatting
10. Python Program to Count Unique Values in Each Group
Problem Statement
Write a Python program to count the number of unique students in each department.
Python Solution
import pandas as pd
data = {
"Department": [
"IT",
"HR",
"IT",
"HR",
"IT"
],
"Name": [
"Rahul",
"Aman",
"Rahul",
"Sneha",
"Priya"
]
}
df = pd.DataFrame(data)
result = df.groupby("Department")["Name"].nunique()
print(result)
Sample Output
Department
HR 2
IT 2
Name: Name, dtype: int64
Explanation
The nunique() function counts distinct values within each group, making it useful for identifying unique records.
Concepts Covered
nunique()- Unique Count
- GroupBy Aggregation
11. Python Program to Apply Custom Aggregation Functions
Problem Statement
Write a Python program to calculate the minimum, maximum, and average marks for each department using custom aggregation.
Python Solution
import pandas as pd
data = {
"Department": ["IT", "HR", "IT", "HR"],
"Marks": [85, 90, 78, 88]
}
df = pd.DataFrame(data)
result = df.groupby("Department").agg(
{
"Marks": ["min", "max", "mean"]
}
)
print(result)
Sample Output
Marks
min max mean
Department
HR 88 90 89.0
IT 78 85 81.5
Explanation
The agg() function accepts a dictionary to apply multiple aggregation functions to selected columns.
Concepts Covered
agg()- Custom Aggregation
- Multiple Functions
12. Python Program to Retrieve the First Record from Each Group
Problem Statement
Write a Python program to display the first student from each department.
Python Solution
import pandas as pd
data = {
"Department": ["IT", "HR", "IT", "HR"],
"Name": ["Rahul", "Aman", "Priya", "Sneha"]
}
df = pd.DataFrame(data)
result = df.groupby("Department").first()
print(result)
Sample Output
Name
Department
HR Aman
IT Rahul
Explanation
The first() function returns the first row from each group.
Concepts Covered
first()- GroupBy
- First Record
13. Python Program to Retrieve the Last Record from Each Group
Problem Statement
Write a Python program to display the last student from each department.
Python Solution
import pandas as pd
data = {
"Department": ["IT", "HR", "IT", "HR"],
"Name": ["Rahul", "Aman", "Priya", "Sneha"]
}
df = pd.DataFrame(data)
result = df.groupby("Department").last()
print(result)
Sample Output
Name
Department
HR Sneha
IT Priya
Explanation
The last() function retrieves the last row from every group.
Concepts Covered
last()- GroupBy
- Last Record
14. Python Program to Calculate Standard Deviation for Each Group
Problem Statement
Write a Python program to calculate the standard deviation of marks for each department.
Python Solution
import pandas as pd
data = {
"Department": ["IT", "HR", "IT", "HR"],
"Marks": [85, 90, 78, 88]
}
df = pd.DataFrame(data)
result = df.groupby("Department")["Marks"].std()
print(result)
Sample Output
Department
HR 1.414214
IT 4.949747
Name: Marks, dtype: float64
Explanation
The std() function calculates the standard deviation for each group, showing how much the values vary from the mean.
Concepts Covered
std()- Group Statistics
- Standard Deviation
15. Python Program to Calculate Variance for Each Group
Problem Statement
Write a Python program to calculate the variance of marks for each department.
Python Solution
import pandas as pd
data = {
"Department": ["IT", "HR", "IT", "HR"],
"Marks": [85, 90, 78, 88]
}
df = pd.DataFrame(data)
result = df.groupby("Department")["Marks"].var()
print(result)
Sample Output
Department
HR 2.0
IT 24.5
Name: Marks, dtype: float64
Explanation
The var() function calculates the variance of values within each group, measuring data dispersion.
Concepts Covered
var()- Variance
- Group Statistics
16. Python Program to Apply Multiple Aggregation Functions on Multiple Columns
Problem Statement
Write a Python program to calculate the average Age and the maximum Marks for each department.
Python Solution
import pandas as pd
data = {
"Department": ["IT", "HR", "IT", "HR"],
"Age": [20, 21, 19, 22],
"Marks": [85, 90, 78, 88]
}
df = pd.DataFrame(data)
result = df.groupby("Department").agg(
{
"Age": "mean",
"Marks": "max"
}
)
print(result)
Sample Output
Age Marks
Department
HR 21.5 90
IT 19.5 85
Explanation
The agg() function can perform different aggregation operations on multiple columns simultaneously.
Concepts Covered
agg()- Multiple Columns
- GroupBy Aggregation
17. Python Program to Calculate the Total Salary by Department
Problem Statement
Write a Python program to calculate the total salary of employees in each department.
Python Solution
import pandas as pd
data = {
"Department": ["IT", "HR", "IT", "HR", "Sales"],
"Salary": [50000, 45000, 55000, 47000, 60000]
}
df = pd.DataFrame(data)
result = df.groupby("Department")["Salary"].sum()
print(result)
Sample Output
Department
HR 92000
IT 105000
Sales 60000
Name: Salary, dtype: int64
Explanation
The groupby() function groups employees by department, while the sum() function calculates the total salary for each group.
Concepts Covered
groupby()sum()- Salary Analysis
Chapter Summary
In this chapter, you learned how to use the GroupBy feature in Pandas to organize and summarize data efficiently. You explored grouping data by one or multiple columns, calculating totals, averages, minimums, maximums, standard deviation, variance, unique counts, and applying multiple aggregation functions. You also learned how to retrieve the first and last records of each group and reset indexes after grouping. These techniques are widely used in reporting, business intelligence, finance, HR analytics, and data science projects.
Key Takeaways
groupby()divides data into meaningful groups.sum(),mean(),min(),max(),count(), andsize()summarize grouped data.agg()applies multiple aggregation functions at once.nunique()counts unique values within each group.first()andlast()retrieve the first and last records from groups.std()calculates standard deviation.var()calculates variance.reset_index()converts grouped indexes into normal columns.- Grouping by multiple columns enables detailed hierarchical analysis.
- GroupBy is one of the most frequently used features in real-world data analysis.
Frequently Asked Questions (FAQs)
1. What is the purpose of the groupby() function in Pandas?
The groupby() function divides data into groups based on one or more columns, allowing aggregation and analysis.
df.groupby("Department")
2. Which function calculates the average value for each group?
Use the mean() function.
df.groupby("Department")["Marks"].mean()
3. How do you calculate multiple statistics at once?
Use the agg() function.
df.groupby("Department")["Marks"].agg(
["sum", "mean", "max"]
)
4. How do you count unique values in each group?
Use the nunique() function.
df.groupby("Department")["Name"].nunique()
5. What is the difference between count() and size()?
count()counts only non-missing values.size()counts all rows, including rows with missing values.
6. Why is reset_index() used after GroupBy?
reset_index() converts grouped indexes back into normal columns, making the output easier to read and export.
result.reset_index()
7. Can groupby() work with multiple columns?
Yes. You can group data using multiple columns.
df.groupby(
["Department", "Gender"]
)
8. Why is GroupBy important in Pandas?
GroupBy simplifies data summarization and reporting by allowing calculations such as totals, averages, counts, and other statistics for different categories. It is widely used in business analytics, dashboards, financial reporting, HR analytics, and machine learning data preparation.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
