GroupBy with custom functions
📖 Scenario: You work in a small bookstore. You have sales data for different book genres and the number of books sold each day. You want to analyze this data to find out the total books sold per genre and also find the difference between the highest and lowest sales for each genre.
🎯 Goal: Build a program that groups the sales data by book genre and applies a custom function to calculate the sales range (difference between max and min sales) for each genre.
📋 What You'll Learn
Create a pandas DataFrame with sales data for book genres and daily sales.
Define a custom function to calculate the range of sales (max - min).
Use
groupby on the genre column and apply the custom function.Print the resulting DataFrame showing total sales and sales range per genre.
💡 Why This Matters
🌍 Real World
Bookstores and retail stores often analyze sales data by category to understand performance and stock needs.
💼 Career
Data analysts and data scientists use grouping and custom functions to summarize and extract insights from sales and business data.
Progress0 / 4 steps