Quick Overview

This question evaluates proficiency in data manipulation using pandas, including relational joins and aggregation to produce country-level summary statistics like totals and averages.

Create Country-Level Spend Report Using Pandas

Company: Amazon

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Technical Screen

users +---------+---------+ | user_id | country | +---------+---------+ | 1 | US | | 2 | CA | | 3 | US | +---------+---------+ ​ transactions +--------+---------+--------+ | txn_id | user_id | amount | +--------+---------+--------+ | 10 | 1 | 30.5 | | 11 | 1 | 15.0 | | 12 | 2 | 20.0 | +--------+---------+--------+ ##### Scenario E-commerce analytics team has separate user and transaction tables and needs country-level spend reporting. ##### Question Using pandas, merge the users and transactions DataFrames on user_id; keep only rows with matching users. Group the merged result by country, computing total and average amount, and return a DataFrame sorted by total spend descending. ##### Hints Apply pandas merge, groupby, agg, and sort_values correctly.

Overview: This question evaluates proficiency in data manipulation using pandas, including relational joins and aggregation to produce country-level summary statistics like totals and averages.

You are given a users table and a transactions table. Write an SQL query to: 1) Perform an INNER JOIN between users and transactions on user_id (keeping only users that have at least one transaction). 2) Group the joined data by country. 3) For each country, compute the total transaction amount and the average transaction amount. 4) Return the result ordered by total transaction amount in descending order. Name the output columns country, total_amount, and average_amount.

Tables

users(user_id INTEGER, country VARCHAR(2))

transactions(txn_id INTEGER, user_id INTEGER, amount DECIMAL(10,2))

Hints

  1. Use an INNER JOIN on users.user_id = transactions.user_id to keep only matching rows.
  2. Group by u.country and compute SUM(t.amount) and AVG(t.amount).
AmazonData ScientistmediumData Manipulation (SQL/Python)

Country-level total and average transaction amounts

You are given a users table and a transactions table. Write an SQL query to:

  1. Perform an INNER JOIN between users and transactions on user_id (keeping only users that have at least one transaction).
  2. Group the joined data by country.
  3. For each country, compute the total transaction amount and the average transaction amount.
  4. Return the result ordered by total transaction amount in descending order. Name the output columns country, total_amount, and average_amount.

Expected Output

One row per country showing total and average transaction amounts for users with at least one transaction, ordered by total_amount in descending order.

Tables

userstransactions
users
ColumnType
user_idINTEGER
countryVARCHAR(2)
transactions
ColumnType
txn_idINTEGER
user_idINTEGER
amountDECIMAL(10,2)

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions