0
0
Data Analysis Pythondata~30 mins

groupby() basics in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
groupby() basics
📖 Scenario: You work in a small store and have a list of sales records. Each record shows the product sold and the quantity sold. You want to find out how many items of each product were sold in total.
🎯 Goal: Use the groupby() function from pandas to group sales by product and calculate the total quantity sold for each product.
📋 What You'll Learn
Create a pandas DataFrame with sales data
Create a variable for grouping the data by product
Use groupby() and sum() to calculate total quantities
Print the grouped result showing total quantity per product
💡 Why This Matters
🌍 Real World
Stores and businesses often need to summarize sales data by product to understand what sells best.
💼 Career
Data analysts and scientists use grouping and aggregation to prepare reports and find insights from data.
Progress0 / 4 steps
1
Create the sales data DataFrame
Import pandas as pd and create a DataFrame called sales with these exact columns and rows:
product: 'apple', 'banana', 'apple', 'orange', 'banana'
quantity: 10, 5, 7, 8, 3
Data Analysis Python
Hint

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

2
Create a group variable by product
Create a variable called grouped that groups the sales DataFrame by the product column using groupby().
Data Analysis Python
Hint

Use sales.groupby('product') and assign it to grouped.

3
Calculate total quantity per product
Create a variable called total_quantity that uses the grouped variable to calculate the sum of quantity for each product.
Data Analysis Python
Hint

Use grouped['quantity'].sum() to get the total quantity per product.

4
Print the total quantity per product
Print the total_quantity variable to display the total quantity sold for each product.
Data Analysis Python
Hint

Use print(total_quantity) to show the result.