Pandas interview question: RFM customer segmentation

HardMedian and percentiles

Problem

Shared data rules

Split all customers into RFM segments by the rules below. For each segment, find the number of customers, the average number of orders and the share of all customers’ revenue.

Formulas

Example

Input: orders
order_iduser_idcreated_atstatusamount_usd
10112026-09-25 12:00delivered100
10222026-07-10 12:00delivered16
10332026-08-15 12:00delivered60
10432026-09-01 12:00delivered60
10532026-09-20 12:00delivered60
10642026-07-20 12:00delivered18
10742026-08-01 12:00delivered22
Output
segmentbuyersavg_ordersrevenue_share
new110.2976
hibernating110.0476
loyal130.5357
others120.119

By last order, customers 2, 4, 3, 1 get R = 1, 2, 3, 4. Customer 1 is 'new' (R = 4, F = 1), 2 is 'hibernating', 3 is 'loyal' (F = 3), 4 is 'others' (R = 2, F = 2).

Notes

Solve it free

Your code runs on real pandas in your browser. No sign-up needed.

Skills it tests

More interview questions

All questions