Skip to content
0

Reporting on sales data

Now let's now move on to the competition and challenge.

📖 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.

from IPython import display
display.Image("Motorcycle Parts.jpg")

💾 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.
import pandas as pd
df = pd.read_csv('data/sales_data.csv', parse_dates=['date'])
df.head()

💪 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.

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

df.groupby('payment').agg({'total':sum})

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

df.groupby('product_line').agg({'unit_price':'mean'})

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

df.groupby('payment').agg({'total':sum}).plot(kind='bar'),df.groupby('product_line').agg({'unit_price':'mean'}).plot(kind='bar')

4. [Optional] Investigate further (e.g., average purchase value by client type, total purchase value by product line, etc.)

from numpy import median 
cat_cols = df.select_dtypes(object).columns
for grouper in cat_cols:
    print(f"By {grouper}:")
    display.display(df.groupby(grouper).agg([sum, 'mean','median']))

5. Summarize your findings.


print(f"There are total {len(df.columns)} out of which following {len(cat_cols)}:\n{chr(10).join(cat_cols.to_list())}\nare categorical rest are numeric.\nCategorical columns have following unique values:\n{ {col:df[col].nunique() for col in cat_cols} }\n Numerical Columns can be summarized as:")
df.describe()