0
0
Data Analysis Pythondata~20 mins

Sales data analysis pattern in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sales Data Analysis Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
data_output
intermediate
2:00remaining
Calculate total sales per product

You have a sales dataset with columns: product, units_sold, and unit_price. What is the total sales amount per product?

Data Analysis Python
import pandas as pd

data = pd.DataFrame({
    'product': ['A', 'B', 'A', 'C', 'B'],
    'units_sold': [10, 5, 7, 3, 8],
    'unit_price': [2.5, 3.0, 2.5, 4.0, 3.0]
})

# Calculate total sales per product
sales = data.copy()
sales['total'] = sales['units_sold'] * sales['unit_price']
result = sales.groupby('product')['total'].sum().sort_index()
A{'A': 42.5, 'B': 39.0, 'C': 12.0}
B{'A': 17.5, 'B': 13.0, 'C': 12.0}
C{'A': 42.5, 'B': 26.0, 'C': 12.0}
D{'A': 42.5, 'B': 39.0, 'C': 15.0}
Attempts:
2 left
💡 Hint

Multiply units sold by unit price for each row, then sum totals by product.

🧠 Conceptual
intermediate
1:30remaining
Understanding sales data time grouping

You want to analyze monthly sales totals from daily sales data. Which step is essential before grouping by month?

AConvert the sales amount to integers
BConvert the date column to datetime type
CRemove duplicate sales records
DSort the data by product name
Attempts:
2 left
💡 Hint

Grouping by month requires working with date information properly.

🔧 Debug
advanced
2:00remaining
Fix the error in sales aggregation code

What error does this code raise?

import pandas as pd
data = pd.DataFrame({'product': ['A', 'B'], 'units_sold': [5, 3]})
total_sales = data.groupby('product')['units_sold' * 2].sum()
Data Analysis Python
import pandas as pd
data = pd.DataFrame({'product': ['A', 'B'], 'units_sold': [5, 3]})
total_sales = data.groupby('product')['units_sold' * 2].sum()
AKeyError: 'units_sold * 2'
BTypeError: unsupported operand type(s) for *: 'str' and 'int'
CSyntaxError: invalid syntax
DValueError: No numeric types to aggregate
Attempts:
2 left
💡 Hint

Look carefully at the expression inside the brackets.

visualization
advanced
2:30remaining
Identify the correct sales trend plot

You have monthly sales totals for three products. Which plot code correctly shows a line chart with months on the x-axis and sales on the y-axis for each product?

Data Analysis Python
import pandas as pd
import matplotlib.pyplot as plt

sales = pd.DataFrame({
    'month': ['Jan', 'Feb', 'Mar'],
    'A': [100, 120, 130],
    'B': [90, 110, 115],
    'C': [80, 85, 90]
})
A
plt.plot(sales['month'], sales['A'], sales['B'], sales['C'])
plt.show()
B
sales.plot(y='month', x=['A', 'B', 'C'], kind='line')
plt.show()
C
sales.plot(x='month', y='A', kind='bar')
plt.show()
D
sales.plot(x='month', y=['A', 'B', 'C'], kind='line')
plt.show()
Attempts:
2 left
💡 Hint

Check which argument sets the x-axis and which sets the y-axis in plot().

🚀 Application
expert
3:00remaining
Find the product with highest average daily sales

Given daily sales data with columns date, product, and units_sold, which code finds the product with the highest average daily units sold?

Data Analysis Python
import pandas as pd

data = pd.DataFrame({
    'date': ['2024-01-01', '2024-01-01', '2024-01-02', '2024-01-02', '2024-01-03'],
    'product': ['A', 'B', 'A', 'B', 'A'],
    'units_sold': [10, 5, 15, 7, 20]
})
data['date'] = pd.to_datetime(data['date'])
A
avg_sales = data.groupby('date')['units_sold'].mean()
result = avg_sales.idxmax()
B
avg_sales = data.groupby('product')['units_sold'].mean()
result = avg_sales.idxmax()
C
avg_sales = data.groupby(['product', 'date'])['units_sold'].sum().groupby('product').mean()
result = avg_sales.idxmax()
D
avg_sales = data.groupby('product')['units_sold'].sum()
result = avg_sales.idxmax()
Attempts:
2 left
💡 Hint

Calculate daily totals per product first, then average those daily totals per product.