0
0
Pandasdata~30 mins

groupby() basics in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Using groupby() Basics in pandas
📖 Scenario: You work in a small store. You have a list of sales with product names and amounts sold. You want to find out how many units were sold for each product.
🎯 Goal: Build a small program that groups sales by product name and sums the amounts sold for each product using groupby() in pandas.
📋 What You'll Learn
Create a pandas DataFrame with sales data
Create a variable for grouping by product
Use groupby() and sum() to get total sales per product
Print the grouped result
💡 Why This Matters
🌍 Real World
Grouping data by categories and summarizing is common in sales, finance, and many fields to understand totals or averages.
💼 Career
Data analysts and scientists often use <code>groupby()</code> in pandas to prepare data for reports and insights.
Progress0 / 4 steps
1
Create the sales DataFrame
Create a pandas DataFrame called sales with two columns: 'product' and 'amount'. Use these exact rows: 'apple', 10, 'banana', 5, 'apple', 7, 'banana', 3, 'orange', 8.
Pandas
Need a hint?

Use pd.DataFrame with a dictionary containing the two columns and their values.

2
Set the grouping variable
Create a variable called group_column and set it to the string 'product'. This will be used to group the sales data by product.
Pandas
Need a hint?

Just assign the string 'product' to the variable group_column.

3
Group and sum the sales
Create a variable called grouped_sales that groups the sales DataFrame by group_column and sums the amount for each product.
Pandas
Need a hint?

Use sales.groupby(group_column)['amount'].sum() to get total sales per product.

4
Print the grouped sales result
Print the grouped_sales variable to show the total amount sold for each product.
Pandas
Need a hint?

Use print(grouped_sales) to display the result.