Skip to content

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.

Left input
  1. Input
    File Input
    orders.csv
  2. Transform
    Group By Aggregate
    customer_id · sum(amount) · count(order_id)
  3. Clean
    Rename Columns
    amount → total_spent · order_id → num_orders
Right input
  1. Input
    File Input
    customers.csv
  1. Join
    Join / Merge
    on: customer_id · how: left
  2. Transform
    Bin Column
    total_spent → tier, 3 equal-width bins
  3. Clean
    Sort Rows
    total_spent, descending
  4. Output
    File Output
    segments.csv

Sample data

customers.csv:

csv
customer_id,name,country
1,Ada,UK
2,Grace,US
3,Linus,FI
4,Margaret,US

orders.csv:

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.00

Download customers.csv and orders.csv, then upload both on the Datasets page.

Build the flow

  1. File Input (orders): File type CSV, dataset orders.csv.
  2. Group By Aggregate: Group by columns = customer_id. Under Aggregations, set amount → sum and order_id → count. The six orders become four rows, one per customer.
  3. Rename Columns: amount → total_spent, order_id → num_orders.
  4. File Input (customers): File type CSV, dataset customers.csv.
  5. 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.
  6. Bin Column: Column total_spent, New column tier, Method Equal width, Number of bins 3, Custom labels Bronze, Silver, Gold.
  7. Sort Rows: Sort by columns total_spent, Order Descending.
  8. 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.

Equal-width bins split the 15 to 500 range at 176.67 and 338.33, putting Linus and Ada in Bronze, Margaret in Silver and Grace in Gold

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

Full flow
Before
order_idcustomer_idamount
5001140
5002160
50032500
5004315
50054220
5006430
6 rows · 3 cols
After
customer_idtotal_spentnewnum_ordersnewnamecountrytiernew
25001GraceUSGold
42502MargaretUSSilver
11002AdaUKBronze
3151LinusFIBronze
4 rows · 6 cols

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: every customer_id in the orders matched a customer.
  • num_orders adds up to 6 and total_spent adds 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:

python
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_id on one side and id on the other, tick Keys have different names on each side and set Left key(s) and Right key(s).

Next steps