Customer Segmentation
You have a customer list and a separate order history, and you want each customer labelled Bronze, Silver or Gold by how much they spent. The order file has one row per order, so you total it per customer first, then join the totals to the customer list and bin them into three tiers.
You'll use: two File Inputs → Group By Aggregate → Rename Columns → Join / Merge → Bin Column → Sort Rows → File Output.
The flow has two branches. The left branch reads and totals the orders; the right branch is the customer list. They meet at the Join / Merge node.
- InputFile Inputorders.csv
- TransformGroup By Aggregatecustomer_id · sum(amount) · count(order_id)
- CleanRename Columnsamount →
total_spent · order_id → num_orders
- InputFile Inputcustomers.csv
- JoinJoin /
Merge on: customer_id · how: left - TransformBin Columntotal_spent →
tier, 3 equal-width bins - CleanSort Rowstotal_spent, descending
- OutputFile Outputsegments.csv
Sample data
customers.csv:
customer_id,name,country
1,Ada,UK
2,Grace,US
3,Linus,FI
4,Margaret,USorders.csv:
order_id,customer_id,amount
5001,1,40.00
5002,1,60.00
5003,2,500.00
5004,3,15.00
5005,4,220.00
5006,4,30.00Download customers.csv and orders.csv, then upload both on the Datasets page.
Build the flow
- File Input (orders): File type CSV, dataset
orders.csv. - Group By Aggregate: Group by columns =
customer_id. Under Aggregations, setamount→sumandorder_id→count. The six orders become four rows, one per customer. - Rename Columns:
amount→total_spent,order_id→num_orders. - File Input (customers): File type CSV, dataset
customers.csv. - Join / Merge: connect the Rename Columns output to the left handle
and the customers input to the right handle. Set Join type to
Left and Join on to
customer_id. Each customer total now carries the customer's name and country. - Bin Column: Column
total_spent, New columntier, Method Equal width, Number of bins3, Custom labelsBronze, Silver, Gold. - Sort Rows: Sort by columns
total_spent, Order Descending. - File Output: File type CSV, Dataset name
segments.
Aggregating before the join keeps the join one-to-one: four totals meet four customers. If you joined the raw orders instead, Ada and Margaret would each appear twice and you would still need a group-by afterwards.
Prefer to describe it?
With a provider key set up, upload the sample data, open a blank flow in the same project and ask the Flow assistant, for example: "total each customer's orders, join them to the customers file and bin the totals into tiers." Review the proposed nodes and settings, then choose Apply and save. We ran this prompt on the sample data; see what the assistant built. Its flow can differ from the steps above in node order and names, so check each setting. The assistant never runs a flow, so click Run yourself.
How the tiers are cut
With Equal width, Bin Column splits the range from the smallest total to the largest into three bins of the same width. The totals run from 15 to 500, so each bin is 161.67 wide and the edges sit at 176.67 and 338.33.
One large customer stretches the range. Grace's 500 makes the Gold bin start at 338.33, so Margaret's 250, the second-highest total, lands in Silver.
Result
| order_id | customer_id | amount |
|---|---|---|
| 5001 | 1 | 40 |
| 5002 | 1 | 60 |
| 5003 | 2 | 500 |
| 5004 | 3 | 15 |
| 5005 | 4 | 220 |
| 5006 | 4 | 30 |
| customer_id | total_spentnew | num_ordersnew | name | country | tiernew |
|---|---|---|---|---|---|
| 2 | 500 | 1 | Grace | US | Gold |
| 4 | 250 | 2 | Margaret | US | Silver |
| 1 | 100 | 2 | Ada | UK | Bronze |
| 3 | 15 | 1 | Linus | FI | Bronze |
The left input's columns come first, then the customer columns, then the new
tier column.
Check the result
- Four rows, one per customer, and no empty
name: everycustomer_idin the orders matched a customer. num_ordersadds up to 6 andtotal_spentadds up to 865.00, the same as the order file.- The tiers are Gold, Silver, Bronze, Bronze from top to bottom.
An empty name means an order references a customer that is not in
customers.csv. A Left join keeps that row so you can spot it; an Inner
join would drop it silently.
Exported Python
Click Export in the editor toolbar and open the pandas tab:
import pandas as pd
df_orders = pd.read_csv('orders.csv')
df_customers = pd.read_csv('customers.csv')
df_orders = (
df_orders.groupby('customer_id', dropna=False)
.agg({'amount': 'sum', 'order_id': 'count'})
.reset_index()
.rename(columns={'amount': 'total_spent', 'order_id': 'num_orders'})
)
df_1 = df_orders.merge(df_customers, on='customer_id', how='left')
df_1 = (
df_1.assign(tier=pd.cut(df_1['total_spent'], bins=3, labels=['Bronze', 'Silver', 'Gold']).astype('string'))
.sort_values('total_spent', ascending=False)
)
df_1.to_csv('segments.csv', index=False)Each input frame is named after its dataset. The two branches keep their own
variables (df_orders, df_customers) because both must exist when merge
runs; the join output gets a numbered name, df_1. Run the script next to the
two CSV files and segments.csv matches the result table. The polars tab
computes the same bin edges explicitly and writes the same file.
Pitfalls
- Equal width or quantile. Equal width splits the value range evenly, so one outlier can leave a bin almost empty. Quantile puts roughly the same number of customers in each bin instead. Its edges come from the data: with four customers they fall exactly on Ada's 100.00 and Margaret's 250.00. A total equal to an edge goes into the lower bin, so Ada is Bronze and Margaret is Silver on both engines. On a sample this small one changed order can move a customer to another tier, so check the preview.
- Labels must match the bin count. Three bins need exactly three labels,
listed from the lowest bin to the highest. With two labels for three bins the
run is refused before it starts:
needs one label per bin: 3 bins, 2 labels. - Keys with different names. If the key is
customer_idon one side andidon the other, tick Keys have different names on each side and set Left key(s) and Right key(s).