cswork / libraryOpen practice workspace

Coding questions · TikTok

Analyze and Clean European Ecommerce Sales Data

This question evaluates proficiency in data manipulation and analysis with Pandas, covering time-window filtering, regional subsetting, deduplication, missing-value handling, aggregation, and visual summarization.

sales

+----------+-------------+------------+---------+---------+----------------+
| order_id | customer_id | order_date | country | revenue | product_category|
+----------+-------------+------------+---------+---------+----------------+
| 1001     | 501         | 2023-08-01 | US      | 250.00  | Accessories     |
| 1002     | 502         | 2023-08-02 | UK      | 180.00  | Apparel         |
| 1003     | 503         | 2023-08-02 | DE      | 310.00  | Electronics     |
| 1004     | 501         | 2023-08-03 | US      | 95.00   | Accessories     |
| 1005     | 504         | 2023-08-04 | CA      | 420.00  | Electronics     |
+----------+-------------+------------+---------+---------+----------------+
Scenario

You receive a raw ecommerce sales table exported from multiple regional warehouses and must create a clean dataset for analysts.

Question

Use Pandas to retrieve all European orders placed in the last 30 days and compute their total revenue. 2. Identify and remove duplicated orders, then explain how you would impute any missing revenue values. 3. Produce summary statistics and two visual insights that highlight sales trends by product_category.

Hints

Pandas filtering, groupby-agg, isnull, fillna, duplicated, matplotlib/seaborn.

European Orders Cleaning and Revenue Summary

Using SQL on the sales table, return a deduplicated set of European orders (defined as country IN ('UK','DE')) between '2025-05-03' and '2025-06-01', impute any missing revenue using customer-level average then category-level average then global average, and produce product_category-level summary statistics along with an overall total revenue.

Table sales

columntypeconstraints
order_idINTEGERNOT NULL
customer_idINTEGERNOT NULL
order_dateDATENOT NULL
countryVARCHAR(2)NOT NULL
revenueDECIMAL(10,2)
product_categoryVARCHAR(50)

Sample data:

countrycustomer_idorder_dateorder_idproduct_categoryrevenue
US5012025-05-101001Accessories250
UK5022025-05-151002Apparel180
DE5032025-05-161003Electronics310
US5012025-05-201004Accessories95
CA5042025-05-251005Electronics420

Expected output

Using the explicit date range from '2025-05-03' to '2025-06-01', the European orders (country IN ('UK','DE')) are order_id 1002 (UK, Apparel) and 1003 (DE, Electronics). In the sample data there are no duplicate order_ids and no missing revenue, so deduplication and imputation do not change values. The query should output summary statistics by product_category for these orders, plus an overall total row with NULL product_category.

avg_revenueorder_countproduct_categorytotal_revenue
3101Electronics310
1801Apparel180
2452NULL490

Hints

  1. Filter to European orders with WHERE country IN ('UK','DE') and the date range order_date BETWEEN '2025-05-03' AND '2025-06-01'.
  2. Remove duplicate order_ids using ROW_NUMBER() OVER (PARTITION BY order_id ORDER BY order_date DESC, revenue DESC NULLS LAST) and keep only rn = 1.
  3. Impute revenue with COALESCE(revenue, AVG(revenue) OVER (PARTITION BY customer_id), AVG(revenue) OVER (PARTITION BY product_category), AVG(revenue) OVER ()).
  4. Compute summary statistics by product_category and an overall total using GROUP BY GROUPING SETS or ROLLUP.