Using transform() for Group-Level Operations in pandas
📖 Scenario: You work in a retail company analyzing sales data. You want to understand how each product's sales compare to the average sales of its category.
🎯 Goal: Build a pandas DataFrame with sales data, then use transform() to calculate the average sales per category and add it as a new column.
📋 What You'll Learn
Create a pandas DataFrame named
sales_data with columns 'Category' and 'Sales' using the exact data provided.Create a variable named
group_column and set it to the string 'Category'.Use
groupby() on sales_data with group_column and apply transform() with 'mean' to calculate average sales per category, storing the result in a new column 'Average_Sales'.Print the
sales_data DataFrame to display the final result.💡 Why This Matters
🌍 Real World
Retail analysts often compare individual product sales to category averages to identify strong or weak performers.
💼 Career
Data scientists and analysts use group-level operations like transform() to create features and insights for reports and machine learning.
Progress0 / 4 steps