Editor

Create a Summary Report

Intermediate · Write the code · from Build a Sales Performance Dashboard with KPI Analysis

From the lesson: Step 6: Create a Summary Report Function

Time to combine everything into a single report function. A good report function runs all the analysis and returns organized results. This makes it easy to generate reports for different time periods or datasets.

Read this part of the lesson

Write a function create_report(df) that returns a dictionary with:

  • kpis: dict with total_revenue, transaction_count, avg_order_value
  • top_product: name of the product with highest total revenue
  • top_salesperson: name of the salesperson with highest total revenue
  • best_region: name of the region with highest total revenue
  • Then print the report in a readable format.

    import pandas as pd
    def create_report(df):
    # Your code here
    pass
    data = {
    'product': ['Laptop', 'Phone', 'Laptop', 'Tablet', 'Phone'],
    'salesperson': ['Alice', 'Bob', 'Charlie', 'Alice', 'Bob'],
    'region': ['North', 'South', 'East', 'North', 'South'],
    'quantity': [2, 5, 1, 3, 4],
    'unit_price': [999.99, 699.99, 999.99, 449.99, 699.99]
    }
    df = pd.DataFrame(data)
    df['revenue'] = df['quantity'] * df['unit_price']
    report = create_report(df)
    print(f'Total Revenue: ${report["kpis"]["total_revenue"]:.2f}')
    print(f'Transactions: {report["kpis"]["transaction_count"]}')
    print(f'Avg Order: ${report["kpis"]["avg_order_value"]:.2f}')
    print(f'Top Product: {report["top_product"]}')
    print(f'Top Salesperson: {report["top_salesperson"]}')
    print(f'Best Region: {report["best_region"]}')

    More Sales Report exercises

    All Python exercises · 50 practice problems · Practical Projects quiz