Example input
{
"products": [
{"product_id": 1, "product_name": "Moisturizer", "description": "A hydrating moisturizer for all skin types", "price": "$24.99", "number_of_orders": 500, "margin": "20%"},
{"product_id": 2, "product_name": "Serum", "description": "A rejuvenating serum to reduce fine lines and wrinkles", "price": "$34.99", "number_of_orders": 300, "margin": "25%"},
{"product_id": 3, "product_name": "Cleanser", "description": "A gentle foaming cleanser for sensitive skin", "price": "$19.99", "number_of_orders": 250, "margin": "15%"}]}
Metrics to retrieve:
- Total number of products
- Average price of products
- Average number of orders per product
- Average margin per product
- Total number of orders
- Total revenue
- Total margin
- Average revenue per product
- Average margin per product
- Average price per order
- Average margin per order
- Average revenue per order
- Average revenue per margin
- Average margin per revenue
- Ratio of orders to products
- Ratio of revenue to margin
- Ratio of margin to revenue
- Ratio of orders to revenue
- Ratio of orders to margin
- Ratio of revenue to orders
- Ratio of margin to orders
- Most popular product (by number of orders)
- Most profitable product (by margin)
- Least popular product (by number of orders)
- Least profitable product (by margin)
- Most expensive product
- Cheapest product
python script:
Example output
#import necessary libraries
import json
import pandas as pd
#load the json file
with open('data.json') as f:
data = json.load(f)
#convert the json file to a pandas dataframe
df = pd.DataFrame(data['products'])
#calculate the total number of products
total_products = df['product_id'].count()
#calculate the average price of products
avg_price = df['price'].mean()
#calculate the average number of orders per product
avg_orders_per_product = df['number_of_orders'].mean()
#calculate the average margin per product
avg_margin_per_product = df['margin'].mean()
#calculate the total number of orders
total_orders = df['number_of_orders'].sum()
#calculate the total revenue
total_revenue = (df['price'].astype(str).str.replace('$', '').astype(float) * df['number_of_orders']).sum()
#calculate the total margin
total_margin = (df['margin'].astype(str).str.replace('%', '').astype(float) * df['number_of_orders']).sum()
#calculate the average revenue per product
avg_revenue_per_product = total_revenue / total_products
#calculate the average margin per product
avg_margin_per_product = total_margin / total_products
#calculate the average price per order
avg_price_per_order = total_revenue / total_orders
#calculate the average margin per order
avg_margin_per_order = total_margin / total_orders
#calculate the average revenue per order
avg_revenue_per_order = total_revenue / total_orders
#calculate the average revenue per margin
avg_revenue_per_margin = total_revenue / total_margin
#calculate the average margin per revenue
avg_margin_per_revenue = total_margin / total_revenue
#calculate the ratio of orders to products
ratio_orders_to_products = total_orders / total_products
#calculate the ratio of revenue to margin
ratio_revenue_to_margin = total_revenue / total_margin
#calculate the ratio of margin to revenue
ratio_margin_to_revenue = total_margin / total_revenue
#calculate the ratio of orders to revenue
ratio_orders_to_revenue = total_orders / total_revenue
#calculate the ratio of orders to margin
ratio_orders_to_margin = total_orders / total_margin
#calculate the ratio of revenue to orders
ratio_revenue_to_orders = total_revenue / total_orders
#calculate the ratio of margin to orders
ratio_margin_to_orders = total_margin / total_orders
#calculate the most popular product (by number of orders)
most_popular_product = df.loc[df['number_of_orders'].idxmax()]['product_name']
#calculate the most profitable product (by margin)
most_profitable_product = df.loc[df['margin'].idxmax()]['product_name']
#calculate the least popular product (by number of orders)
least_popular_product = df.loc[df['number_of_orders'].idxmin()]['product_name']
#calculate the least profitable product (by margin)
least_profitable_product = df.loc[df['margin'].idxmin()]['product_name']
#calculate the most expensive product
most_expensive_product = df.loc[df['price'].idxmax()]['product_name']
#calculate the cheapest product
cheapest_product = df.loc[df['price'].idxmin()]['product_name']
#print the results
print('Total number of products:', total_products)
print('Average price of products:', avg_price)
print('Average number of orders per product:', avg_orders_per_product)
print('Average margin per product:', avg_margin_per_product)
print('Total number of orders:', total_orders)
print('Total revenue:', total_revenue)
print('Total margin:', total_margin)
print('Average revenue per product:', avg_revenue_per_product)
print('Average margin per product:', avg_margin_per_product)
print('Average price per order:', avg_price_per_order)
print('Average margin per order:', avg_margin_per_order)
print('Average revenue per order:', avg_revenue_per_order)
print('Average revenue per margin:', avg_revenue_per_margin)
print('Average margin per revenue:', avg_margin_per_revenue)
print('Ratio of orders to products:', ratio_orders_to_products)
print('Ratio of revenue to margin:', ratio_revenue_to_margin)
print('Ratio of margin to revenue:', ratio_margin_to_revenue)
print('Ratio of orders to revenue:', ratio_orders_to_revenue)
print('Ratio of orders to margin:', ratio_orders_to_margin)
print('Ratio of revenue to orders:', ratio_revenue_to_orders)
print('Ratio of margin to orders:', ratio_margin_to_orders)
print('Most popular product (by number of orders):', most_popular_product)
print('Most profitable product (by margin):', most_profitable_product)
print('Least popular product (by number of orders):', least_popular_product)
print('Least profitable product (by margin):', least_profitable_product)
print('Most expensive product:', most_expensive_product)
print('Cheapest product:', cheapest_product)