Skip to content
Helping a company analyze sales data
  • AI Chat
  • Code
  • Report
  • Reporting on sales data

    📖 Background

    You work in the accounting department of a company that sells motorcycle parts. The company operates three warehouses in a large metropolitan area.

    You’ve recently learned data manipulation and plotting, and suggest helping your colleague analyze past sales data. Your colleague wants to capture sales by payment method. She also needs to know the average unit price for each product line.

    💾 The data

    The sales data has the following fields:
    • "date" - The date, from June to August 2021.
    • "warehouse" - The company operates three warehouses: North, Central, and West.
    • "client_type" - There are two types of customers: Retail and Wholesale.
    • "product_line" - Type of products purchased.
    • "quantity" - How many items were purchased.
    • "unit_price" - Price per item sold.
    • "total" - Total sale = quantity * unit_price.
    • "payment" - How the client paid: Cash, Credit card, Transfer.

    💪 Challenge

    Create a report to answer your colleague's questions. Include:

    1. What are the total sales for each payment method?
    2. What is the average unit price for each product line?
    3. Create plots to visualize findings for questions 1 and 2.
    4. [Optional] Investigate further (e.g., average purchase value by client type, total purchase value by product line, etc.)
    5. Summarize your findings.

    Imports

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns
    import matplotlib.dates as mdates
    
    pd.set_option('display.float_format', '{:,.2f}'.format)
    df = pd.read_csv('data/sales_data.csv', parse_dates=['date'])
    df.head()

    Q1. What are the total sales for each payment method?

    total_sales_per_payment = df.groupby('payment')['total'].sum().reset_index()
    total_sales_per_payment.columns = ['payment_method', 'total_sales']
    total_sales_per_payment = total_sales_per_payment.sort_values(by = 'total_sales', ascending = False)
    total_sales_per_payment

    Q2. What is the average unit price for each product line?

    avg_unit_price = df.groupby('product_line')['unit_price'].mean().reset_index()
    avg_unit_price.columns = ['product_line', 'avg_unit_price']
    avg_unit_price = avg_unit_price.sort_values(by = 'avg_unit_price', ascending = False)
    avg_unit_price

    Q3. Create plots to visualize findings for questions 1 and 2.

    fig, axs = plt.subplots(1, 2, figsize = (16, 6))
    
    sns.barplot(data = total_sales_per_payment, x = 'payment_method', y = 'total_sales', ax = axs[0])
    sns.barplot(data = avg_unit_price, x = 'product_line', y = 'avg_unit_price', ax = axs[1])
    
    axs[0].set_title("Total sales by payment method")
    
    axs[1].tick_params(axis = 'x', rotation = 90)
    axs[1].set_title("Average unit price by product line")
    
    plt.tight_layout()

    Q4. Investigate further (e.g., average purchase value by client type, total purchase value by product line, etc.)

    Q4.1 What is the average purchase value by client type?

    avg_purchase_by_client = df.groupby('client_type')['total'].mean().sort_values(ascending = False).reset_index()
    avg_purchase_by_client.columns = ['client_type', 'avg_purchase_value']
    avg_purchase_by_client