0
0
Pandasdata~30 mins

Why advanced grouping matters in Pandas - See It in Action

Choose your learning style9 modes available
Why advanced grouping matters
📖 Scenario: Imagine you work at a small online store. You have sales data for different products sold in different months. You want to understand how much money each product made in each month. This helps you see which products are popular and when.
🎯 Goal: You will create a small sales data table, set up a grouping configuration, group the data by product and month, and then calculate the total sales for each group. Finally, you will display the grouped sales totals.
📋 What You'll Learn
Create a pandas DataFrame called sales_data with columns product, month, and sales using the exact values provided.
Create a list called group_columns containing the column names 'product' and 'month'.
Use the groupby method on sales_data with group_columns and calculate the sum of sales for each group, storing the result in grouped_sales.
Print the grouped_sales DataFrame to show total sales by product and month.
💡 Why This Matters
🌍 Real World
Grouping data by multiple columns is common in business to analyze sales, customer behavior, or inventory by categories and time periods.
💼 Career
Data analysts and scientists use grouping to summarize and report data insights that help companies make decisions.
Progress0 / 4 steps
1
Create the sales data table
Create a pandas DataFrame called sales_data with these exact columns and values: product with ['Pen', 'Pen', 'Notebook', 'Notebook', 'Eraser', 'Eraser'], month with ['Jan', 'Feb', 'Jan', 'Feb', 'Jan', 'Feb'], and sales with [100, 150, 200, 250, 50, 60].
Pandas
Need a hint?

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

2
Set up grouping columns
Create a list called group_columns containing the strings 'product' and 'month' to specify how to group the data.
Pandas
Need a hint?

Make a list with the two column names as strings.

3
Group and sum sales
Use the groupby method on sales_data with group_columns and calculate the sum of the sales column for each group. Store the result in a variable called grouped_sales.
Pandas
Need a hint?

Use sales_data.groupby(group_columns)['sales'].sum() to get total sales per group.

4
Display the grouped sales totals
Print the grouped_sales DataFrame to show total sales for each product in each month.
Pandas
Need a hint?

Use print(grouped_sales) to show the result.