After learning the core concepts of Pandas, the next step is applying them to real-world business problems. In this chapter, you’ll solve practical case-study-based questions similar to those asked in Data Analyst interviews and used in day-to-day business reporting. Pandas Real-World Mini Projects and Case Study Practice Questions with Solutions help to build concepts.
These exercises combine multiple Pandas concepts such as filtering, grouping, sorting, aggregation, missing value handling, indexing, and file operations to analyze real datasets.
1. Python Program to Find the Best-Selling Product
Problem Statement
A retail store has recorded product sales. Write a Python program to identify the product with the highest total sales.
Python Solution
import pandas as pd
data = {
"Product": [
"Laptop",
"Mouse",
"Laptop",
"Keyboard",
"Mouse"
],
"Sales": [
50000,
3000,
45000,
7000,
5000
]
}
df = pd.DataFrame(data)
result = (
df.groupby("Product")["Sales"]
.sum()
.sort_values(ascending=False)
)
print(result)
Sample Output
Product
Laptop 95000
Mouse 8000
Keyboard 7000
Name: Sales, dtype: int64
Explanation
The data is grouped by product, total sales are calculated using sum(), and the result is sorted in descending order to identify the best-selling product.
Concepts Covered
groupby()sum()sort_values()- Business Reporting
2. Python Program to Find the Department with the Highest Average Salary
Problem Statement
An HR department wants to know which department has the highest average salary.
Python Solution
import pandas as pd
data = {
"Department": [
"IT",
"HR",
"Finance",
"IT",
"Finance"
],
"Salary": [
60000,
45000,
70000,
65000,
75000
]
}
df = pd.DataFrame(data)
result = (
df.groupby("Department")["Salary"]
.mean()
.sort_values(ascending=False)
)
print(result)
Sample Output
Department
Finance 72500.0
IT 62500.0
HR 45000.0
Name: Salary, dtype: float64
Explanation
The program groups employees by department and calculates the average salary using mean().
Concepts Covered
groupby()mean()- HR Analytics
3. Python Program to Find the Top 3 Highest-Paid Employees
Problem Statement
Write a Python program to display the top three highest-paid employees.
Python Solution
import pandas as pd
data = {
"Employee": [
"Rahul",
"Priya",
"Aman",
"Sneha",
"Rohan"
],
"Salary": [
50000,
75000,
65000,
70000,
60000
]
}
df = pd.DataFrame(data)
result = (
df.sort_values(
by="Salary",
ascending=False
).head(3)
)
print(result)
Sample Output
Employee Salary
1 Priya 75000
3 Sneha 70000
2 Aman 65000
Explanation
The DataFrame is sorted in descending order of salary, and the top three rows are displayed.
Concepts Covered
sort_values()head()- Business Reporting
4. Python Program to Calculate Monthly Revenue
Problem Statement
An online store wants to calculate total monthly revenue using quantity sold and product price.
Python Solution
import pandas as pd
data = {
"Product": [
"Laptop",
"Mouse",
"Keyboard"
],
"Quantity": [
5,
20,
10
],
"Price": [
50000,
500,
1500
]
}
df = pd.DataFrame(data)
df["Revenue"] = (
df["Quantity"] *
df["Price"]
)
print(df)
Sample Output
Product Quantity Price Revenue
0 Laptop 5 50000 250000
1 Mouse 20 500 10000
2 Keyboard 10 1500 15000
Explanation
Revenue is calculated by multiplying quantity sold with the product price.
Concepts Covered
- Column Operations
- Arithmetic Calculations
- Revenue Analysis
5. Python Program to Find Employees Earning Above the Average Salary
Problem Statement
Write a Python program to display employees whose salary is greater than the company’s average salary.
Python Solution
import pandas as pd
data = {
"Employee": [
"Rahul",
"Priya",
"Aman",
"Sneha"
],
"Salary": [
50000,
75000,
65000,
55000
]
}
df = pd.DataFrame(data)
average_salary = df["Salary"].mean()
result = df[
df["Salary"] > average_salary
]
print(result)
Sample Output
Employee Salary
1 Priya 75000
2 Aman 65000
Explanation
The program first calculates the average salary and then filters employees earning more than the average.
Concepts Covered
mean()- Boolean Indexing
- Business Analytics
6. Python Program to Find the Customer Who Spent the Most
Problem Statement
An e-commerce company wants to identify the customer with the highest total purchase amount.
Python Solution
import pandas as pd
data = {
"Customer": [
"Rahul",
"Aman",
"Rahul",
"Priya",
"Aman"
],
"Amount": [
5000,
3000,
7000,
9000,
4000
]
}
df = pd.DataFrame(data)
result = (
df.groupby("Customer")["Amount"]
.sum()
.sort_values(ascending=False)
)
print(result)
Sample Output
Customer
Rahul 12000
Aman 7000
Priya 9000
Name: Amount, dtype: int64
Explanation
The program groups purchase records by customer, calculates the total amount spent, and sorts the result in descending order.
Concepts Covered
groupby()sum()- Customer Analytics
7. Python Program to Identify the Best Performing Branch
Problem Statement
A company has multiple branches. Write a Python program to find the branch with the highest sales.
Python Solution
import pandas as pd
data = {
"Branch": [
"Delhi",
"Mumbai",
"Delhi",
"Pune",
"Mumbai"
],
"Sales": [
80000,
90000,
70000,
60000,
85000
]
}
df = pd.DataFrame(data)
result = (
df.groupby("Branch")["Sales"]
.sum()
.sort_values(ascending=False)
)
print(result)
Sample Output
Branch
Mumbai 175000
Delhi 150000
Pune 60000
Name: Sales, dtype: int64
Explanation
Sales from each branch are grouped and summed to determine the highest-performing branch.
Concepts Covered
- Branch Performance
groupby()- Business Intelligence
8. Python Program to Calculate Product Profit
Problem Statement
Write a Python program to calculate the profit for each product.
Formula
Profit = Selling Price − Cost Price
Python Solution
import pandas as pd
data = {
"Product": [
"Laptop",
"Mouse",
"Keyboard"
],
"Cost Price": [
40000,
350,
1000
],
"Selling Price": [
50000,
500,
1500
]
}
df = pd.DataFrame(data)
df["Profit"] = (
df["Selling Price"] -
df["Cost Price"]
)
print(df)
Sample Output
Product Cost Price Selling Price Profit
0 Laptop 40000 50000 10000
1 Mouse 350 500 150
2 Keyboard 1000 1500 500
Explanation
Profit is calculated by subtracting the cost price from the selling price.
Concepts Covered
- Arithmetic Operations
- Business Calculations
- Profit Analysis
9. Python Program to Find the Top Selling Category
Problem Statement
Write a Python program to determine which product category generated the highest sales.
Python Solution
import pandas as pd
data = {
"Category": [
"Electronics",
"Furniture",
"Electronics",
"Books"
],
"Sales": [
90000,
50000,
70000,
25000
]
}
df = pd.DataFrame(data)
result = (
df.groupby("Category")["Sales"]
.sum()
.sort_values(ascending=False)
)
print(result)
Sample Output
Category
Electronics 160000
Furniture 50000
Books 25000
Name: Sales, dtype: int64
Explanation
The DataFrame is grouped by category, and total sales are calculated to identify the highest-selling category.
Concepts Covered
- Category Analysis
groupby()- Sales Reporting
10. Python Program to Calculate Employee Bonus
Problem Statement
A company gives a 10% bonus to every employee based on their salary. Write a Python program to calculate the bonus amount.
Python Solution
import pandas as pd
data = {
"Employee": [
"Rahul",
"Priya",
"Aman",
"Sneha"
],
"Salary": [
50000,
70000,
60000,
55000
]
}
df = pd.DataFrame(data)
df["Bonus"] = (
df["Salary"] * 0.10
)
print(df)
Sample Output
Employee Salary Bonus
0 Rahul 50000 5000.0
1 Priya 70000 7000.0
2 Aman 60000 6000.0
3 Sneha 55000 5500.0
Explanation
The bonus is calculated by multiplying each employee’s salary by 10% (0.10).
Concepts Covered
- Column Calculations
- Percentage Calculation
- HR Analytics
11. Python Program to Find the Month with the Highest Sales
Problem Statement
A company wants to identify the month in which it achieved the highest sales.
Python Solution
import pandas as pd
data = {
"Month": [
"January",
"February",
"March",
"April",
"May"
],
"Sales": [
120000,
150000,
140000,
180000,
170000
]
}
df = pd.DataFrame(data)
result = df.loc[
df["Sales"].idxmax()
]
print(result)
Sample Output
Month April
Sales 180000
Name: 3, dtype: object
Explanation
The idxmax() function returns the index of the highest sales value, and loc[] retrieves the complete row.
Concepts Covered
idxmax()loc[]- Sales Analysis
12. Python Program to Find the Employee with the Highest Sales
Problem Statement
Write a Python program to identify the employee who generated the highest sales.
Python Solution
import pandas as pd
data = {
"Employee": [
"Rahul",
"Priya",
"Aman",
"Sneha"
],
"Sales": [
45000,
75000,
68000,
59000
]
}
df = pd.DataFrame(data)
result = df.loc[
df["Sales"].idxmax()
]
print(result)
Sample Output
Employee Priya
Sales 75000
Name: 1, dtype: object
Explanation
The program finds the highest sales value using idxmax() and retrieves the corresponding employee record.
Concepts Covered
idxmax()- Employee Performance
- Business Reporting
13. Python Program to Find the Most Expensive Product
Problem Statement
Write a Python program to display the product with the highest price.
Python Solution
import pandas as pd
data = {
"Product": [
"Laptop",
"Mouse",
"Keyboard",
"Monitor"
],
"Price": [
60000,
800,
2000,
15000
]
}
df = pd.DataFrame(data)
result = df.loc[
df["Price"].idxmax()
]
print(result)
Sample Output
Product Laptop
Price 60000
Name: 0, dtype: object
Explanation
The idxmax() function identifies the row containing the maximum product price.
Concepts Covered
idxmax()- Product Analysis
- Maximum Value
14. Python Program to Generate a Department Salary Report
Problem Statement
Generate a department-wise report showing the total, average, and maximum salary.
Python Solution
import pandas as pd
data = {
"Department": [
"IT",
"IT",
"HR",
"Finance",
"Finance"
],
"Salary": [
50000,
60000,
45000,
70000,
75000
]
}
df = pd.DataFrame(data)
result = (
df.groupby("Department")["Salary"]
.agg(
Total="sum",
Average="mean",
Maximum="max"
)
)
print(result)
Sample Output
Total Average Maximum
Department
Finance 145000 72500.0 75000
HR 45000 45000.0 45000
IT 110000 55000.0 60000
Explanation
The agg() function performs multiple aggregate calculations in a single operation.
Concepts Covered
groupby()agg()- Business Reports
15. Python Program to Build a Simple Sales Dashboard Summary
Problem Statement
Create a summary report showing:
- Total Sales
- Average Sales
- Highest Sales
- Lowest Sales
Python Solution
import pandas as pd
data = {
"Sales": [
12000,
18000,
15000,
20000,
17000
]
}
df = pd.DataFrame(data)
summary = pd.DataFrame(
{
"Metric": [
"Total Sales",
"Average Sales",
"Highest Sales",
"Lowest Sales"
],
"Value": [
df["Sales"].sum(),
df["Sales"].mean(),
df["Sales"].max(),
df["Sales"].min()
]
}
)
print(summary)
Sample Output
Metric Value
0 Total Sales 82000.0
1 Average Sales 16400.0
2 Highest Sales 20000.0
3 Lowest Sales 12000.0
Explanation
This example creates a simple business dashboard by calculating key performance indicators (KPIs) such as total, average, highest, and lowest sales.
Concepts Covered
sum()mean()max()min()- KPI Dashboard
Chapter Summary
In this chapter, you solved 15 real-world business case studies using Pandas. You combined multiple concepts including filtering, grouping, sorting, aggregation, indexing, arithmetic calculations, and reporting to solve practical business problems. These types of questions closely resemble tasks performed by Data Analysts and are frequently asked in interviews and technical assessments.
Key Takeaways
- Combine multiple Pandas functions to solve real business problems.
- Use
groupby()andagg()to generate reports. - Apply filtering to identify top-performing employees, products, and customers.
- Use
idxmax()to locate records with maximum values. - Perform revenue, profit, and bonus calculations using column operations.
- Build KPI summaries using aggregation functions.
- Analyze sales, departments, branches, and customer behavior.
- Create business-ready reports using Pandas.
- Strengthen problem-solving skills through case studies.
- Learn how Pandas is used in real-world data analytics projects.
Frequently Asked Questions (FAQs)
1. Why are real-world Pandas case studies important?
They help you apply Pandas concepts to practical business scenarios and improve problem-solving skills for interviews and projects.
2. How do you find the row containing the highest value?
df.loc[
df["Sales"].idxmax()
]
3. How do you generate department-wise reports?
df.groupby(
"Department"
).agg(
Total=("Salary", "sum"),
Average=("Salary", "mean")
)
4. How do you calculate revenue in Pandas?
df["Revenue"] = (
df["Quantity"] *
df["Price"]
)
5. How do you calculate employee bonuses?
df["Bonus"] = (
df["Salary"] * 0.10
)
6. What is the purpose of agg()?
The agg() function performs multiple aggregation operations such as sum(), mean(), max(), and min() in a single statement.
7. Which Pandas concepts are most commonly used in Data Analyst jobs?
Filtering, sorting, grouping, aggregation, merging, pivot tables, missing value handling, date-time operations, window functions, and business reporting are among the most frequently used concepts.
8. Are these case studies useful for Pandas interviews?
Yes. These questions closely match the practical tasks asked in Data Analyst, Business Analyst, Data Science, and Python developer interviews, making them excellent practice for technical assessments and real-world projects.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
