0
0
Pandasdata~30 mins

GroupBy with custom functions in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
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
1
Create the sales data DataFrame
Create a pandas DataFrame called sales_data with two columns: 'Genre' and 'Sales'. Use these exact rows: 'Fiction', 10, 'Fiction', 15, 'Non-Fiction', 8, 'Non-Fiction', 12, 'Science', 5, 'Science', 7.
Pandas
Need a hint?

Use pd.DataFrame with a dictionary where keys are column names and values are lists of data.

2
Define a custom function to calculate sales range
Define a function called sales_range that takes a pandas Series called group and returns the difference between the maximum and minimum values in group.
Pandas
Need a hint?

Use group.max() and group.min() to find the highest and lowest sales.

3
Group by Genre and apply custom function
Use groupby on sales_data by the 'Genre' column. Calculate the total sales per genre using sum(). Then apply the sales_range function to the 'Sales' column for each genre. Store the total sales in total_sales and the sales range in range_sales.
Pandas
Need a hint?

Use groupby('Genre')['Sales'] then sum() and apply(sales_range).

4
Print the total sales and sales range per genre
Print the total_sales and range_sales Series to display the total books sold and the sales range for each genre.
Pandas
Need a hint?

Use two print() statements to show total_sales and range_sales.