NLR (New-Lapsed-Repeating) Segmentation
New-Lapsed-Repeating (NLR) Segmentation for Customer Lifecycle Classification.
Business Context
Understanding customer lifecycle stages is fundamental to retail strategy. Customers naturally move between periods of activity and inactivity, and identifying these transitions enables targeted interventions at each stage.
The Business Problem
Retailers need to understand which customers are growing the base (new), which are loyal (repeating), and which have stopped purchasing (lapsed). Without this classification, marketing budgets are misallocated — spending acquisition dollars on existing customers or ignoring at-risk customers who are about to churn.
Segment Definitions
Given two time periods (P1 and P2), customers are classified based on where they have a positive aggregated value:
- New: Positive value in P2 only — these customers were acquired in the later period
- Repeating: Positive value in both P1 and P2 — these customers are retained
- Lapsed: Positive value in P1 only — these customers have stopped purchasing
A customer must have a positive aggregated value (> 0) in a period to be considered as
having "bought" in that period. Zero or negative values do not count. Customers with
no positive value in either period are excluded from the results entirely, as they do
not meet the criteria for any segment. When using non-spend aggregation functions like
count or nunique, the threshold still applies but measures transaction presence
rather than spend.
Real-World Applications
New Customers
- Measure acquisition effectiveness period-over-period
- Design onboarding journeys to convert first-time buyers to repeat customers
- Track new customer quality (spend levels, category breadth)
Repeating Customers
- Core loyal base driving consistent revenue
- Cross-sell and upsell opportunities
- Loyalty program engagement and reward optimization
Lapsed Customers
- Win-back campaigns with targeted incentives
- Churn root cause analysis (price sensitivity, assortment gaps)
- Customer lifetime value recalculation and reactivation ROI modeling
NLRSegmentation
Segments customers into New, Repeating, and Lapsed based on presence across two periods.
NLRSegmentation compares customer purchasing activity across two defined time periods (P1 and P2) to classify each customer's lifecycle stage. A customer is considered active in a period only if their aggregated value is strictly positive (> 0). This enables retailers to measure acquisition, retention, and churn rates in a single view, and to size the revenue impact of each lifecycle segment.
The segmentation is commonly used in period-over-period reporting (e.g., year-over-year, quarter-over-quarter) to answer questions such as how many customers were retained, how many were lost, and how many are newly acquired. When combined with spend data, it reveals whether revenue growth is driven by new customer acquisition or by increasing spend from repeating customers.
Attributes:
| Name | Type | Description |
|---|---|---|
table |
Table
|
The underlying ibis Table expression containing the segmentation results. Can be used for further ibis operations before materializing to a DataFrame. |
df |
DataFrame
|
The materialized segmentation results as a pandas DataFrame, indexed by customer_id (and group_col if specified). Cached after first access. |
Example
import pandas as pd from pyretailscience.segmentation.nlr import NLRSegmentation df = pd.DataFrame({ ... "customer_id": [1, 2, 3, 1, 3, 4], ... "unit_spend": [50.0, 100.0, 80.0, 75.0, 60.0, 150.0], ... "year": [2023, 2023, 2023, 2024, 2024, 2024], ... }) seg = NLRSegmentation(df=df, period_col="year", p1_value=2023, p2_value=2024) seg.df[["segment_name"]] segment_name customer_id 1 Repeating 2 Lapsed 3 Repeating 4 New
Source code in pyretailscience/segmentation/nlr.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
df: pd.DataFrame
property
Returns the DataFrame with segment names, indexed by customer_id (and group_col if specified).
__init__(df, period_col, p1_value, p2_value, value_col=None, agg_func='sum', group_col=None)
Segments customers into New, Repeating, and Lapsed based on positive aggregated value across two periods.
A customer is considered to have "bought" in a period only if their aggregated value_col is strictly positive (> 0). Customers are then classified as: - New: positive value in P2 only - Repeating: positive value in both P1 and P2 - Lapsed: positive value in P1 only
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
DataFrame | Table
|
Transaction data. Must contain customer_id, period_col, and value_col columns. |
required |
period_col |
str
|
Column containing period identifiers. |
required |
p1_value |
str | float | Scalar
|
Value in period_col identifying period 1. |
required |
p2_value |
str | float | Scalar
|
Value in period_col identifying period 2. |
required |
value_col |
str | None
|
Column to aggregate for determining customer activity. Defaults to ColumnHelper().unit_spend. |
None
|
agg_func |
str
|
Aggregation function to use when grouping by customer_id. Defaults to "sum". |
'sum'
|
group_col |
str | list[str] | None
|
Column(s) to group by when calculating segments. When specified, segments are calculated within each group independently. Defaults to None. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If required columns are missing from the DataFrame, or if agg_func is not a supported aggregation function. |
Source code in pyretailscience/segmentation/nlr.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |