We analyze sales data to understand how well products are selling and find ways to improve business.
Sales data analysis pattern in Data Analysis Python
import pandas as pd # Load sales data into a DataFrame data = pd.read_csv('sales.csv') # Group data by a column and calculate sums or counts summary = data.groupby('Product')['Sales'].sum() # Sort results summary_sorted = summary.sort_values(ascending=False) # Display or plot results print(summary_sorted)
Use groupby() to group data by categories like product or date.
Use aggregation functions like sum(), mean(), or count() to summarize data.
summary = data.groupby('Product')['Sales'].sum()
monthly_sales = data.groupby('Month')['Sales'].sum()
top_products = data.groupby('Product')['Sales'].sum().sort_values(ascending=False)
This program creates a small sales dataset, groups sales by product, sums them, and shows which product sold the most.
import pandas as pd # Sample sales data as dictionary sales_data = { 'Product': ['Apple', 'Banana', 'Apple', 'Banana', 'Cherry', 'Apple'], 'Month': ['Jan', 'Jan', 'Feb', 'Feb', 'Jan', 'Mar'], 'Sales': [100, 150, 120, 130, 90, 160] } # Create DataFrame sales_df = pd.DataFrame(sales_data) # Group by Product and sum sales product_sales = sales_df.groupby('Product')['Sales'].sum() # Sort products by total sales descending product_sales_sorted = product_sales.sort_values(ascending=False) print(product_sales_sorted)
Make sure your sales data is clean and columns are named consistently.
Grouping and aggregation are powerful tools to summarize large datasets quickly.
You can also use visualization libraries like matplotlib or seaborn to plot sales trends.
Sales data analysis helps understand product performance and trends.
Use groupby() and aggregation functions to summarize sales data.
Sorting results helps identify top-selling products or periods.
Practice
groupby() in sales data analysis?Solution
Step 1: Understand the role of
Thegroupby()groupby()function groups data based on one or more columns, such as product or date.Step 2: Identify its use in sales data
Grouping allows us to summarize sales, like total sales per product or per month.Final Answer:
To summarize sales data by categories like product or date -> Option DQuick Check:
groupby()groups data for summary [OK]
- Thinking groupby deletes data
- Confusing groupby with sorting
- Assuming groupby creates charts
Solution
Step 1: Understand pandas groupby syntax
The correct syntax groups by 'product' column, then sums the 'sales' column.Step 2: Check each option
df.groupby('product')['sales'].sum() correctly usesdf.groupby('product')['sales'].sum(). Others misuse order or columns.Final Answer:
df.groupby('product')['sales'].sum() -> Option BQuick Check:
Group by product, then sum sales [OK]
- Calling sum() before groupby()
- Grouping by sales instead of product
- Selecting columns after sum()
df below, what is the output of df.groupby('product')['sales'].sum()?
product sales 0 A 100 1 B 200 2 A 150 3 B 100 4 C 300
Solution
Step 1: Group sales by product
Sum sales for each product: A = 100+150=250, B = 200+100=300, C = 300.Step 2: Write the output as pandas Series
The result shows sums indexed by product with name 'sales'.Final Answer:
product\nA 250\nB 300\nC 300\nName: sales, dtype: int64 -> Option AQuick Check:
Sum sales per product = 250, 300, 300 [OK]
- Adding sales incorrectly
- Confusing product labels
- Ignoring grouping before sum
df.groupby('month').mean('sales')Solution
Step 1: Check mean() function usage
Themean()method does not accept column names as arguments.Step 2: Correct usage for column selection
Select the 'sales' column before callingmean():df.groupby('month')['sales'].mean().Final Answer:
mean() does not take any arguments; column selection must be before mean() -> Option AQuick Check:
mean() no args; select column first [OK]
- Passing column name inside mean()
- Using groupby with wrong argument type
- Thinking sorting is required before groupby
product, month, and sales. How do you find the top-selling product for each month?Solution
Step 1: Group sales by month and product
Sum sales for each product in each month usinggroupby(['month', 'product'])['sales'].sum().Step 2: Reshape and find top product per month
Useunstack()to make products columns, thenidxmax(axis=1)to find product with max sales per month.Final Answer:
Use df.groupby(['month', 'product'])['sales'].sum().unstack().idxmax(axis=1) -> Option CQuick Check:
Group by month and product, then find max product [OK]
- Ignoring product when finding max sales
- Using max() without grouping by product
- Sorting without aggregation
Ready to prove it?
Pass the test and this pattern moves unknown
