fix(analytics): count orders instead of order lines in RFM

Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com>
This commit is contained in:
Rudy Celekli
2026-10-13 11:47:27 -04:00
parent 4bce55aa8b
commit ffce970fc6
+4 -1
View File
@@ -92,6 +92,9 @@ ORDER BY month DESC;
```
### Customer Segmentation Analysis
Input rows may represent individual order lines. Sum their revenue, and count distinct order IDs per customer when calculating purchase frequency.
```python
import pandas as pd
import numpy as np
@@ -108,7 +111,7 @@ def customer_segmentation_analysis(df):
current_date = df['date'].max()
rfm = df.groupby('customer_id').agg({
'date': lambda x: (current_date - x.max()).days, # Recency
'order_id': 'count', # Frequency
'order_id': 'nunique', # Frequency: distinct orders
'revenue': 'sum' # Monetary
}).rename(columns={
'date': 'recency',