Create and Prepare the Sales DataFrame
Intermediate · Write the code · from Build a Sales Performance Dashboard with KPI Analysis
From the lesson: Step 1: Create the Sales Dataset
Our dataset represents three months of sales transactions for a small electronics company. Each row is a single sale with the date, product, salesperson, region, quantity, and unit price.
Read this part of the lessonCreate a sales DataFrame from the provided data dictionary. Then:
1. Convert the date column to datetime using pd.to_datetime()
2. Add a revenue column (quantity * unit_price)
3. Add a month column that extracts the month name from the date (use .dt.strftime('%B') to get full month names like "January")
4. Print the total number of transactions and the columns list.
import pandas as pd
sales_data = {
'date': ['2024-01-05', '2024-01-15', '2024-02-03', '2024-02-18', '2024-03-01', '2024-03-15'],
'product': ['Laptop', 'Phone', 'Tablet', 'Laptop', 'Phone', 'Tablet'],
'quantity': [2, 5, 3, 1, 4, 2],
'unit_price': [999.99, 699.99, 449.99, 999.99, 699.99, 449.99]
}
# Create DataFrame and add columns
More Sales Report exercises
- Calculate Sales KPIsIntermediate
- Revenue by Product and RegionIntermediate
- Find Top PerformersIntermediate
- Month-over-Month GrowthIntermediate
- Create a Summary ReportIntermediate
- Generate a Formatted ReportIntermediate
All Python exercises · 50 practice problems · Practical Projects quiz