Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Behavioral Segmentation with Python | Rule-Based Customer Segmentation
Business Analytics and Decision Making with Python

bookBehavioral Segmentation with Python

Behavioral segmentation is a powerful technique for understanding how customers interact with your business based on their actions, rather than just their characteristics. By analyzing behavioral patterns—such as purchase frequency, average order value, and recency of transactions—you can group customers according to how they engage with your product or service. This approach enables more precise personalization and targeting, as you can tailor communications, offers, and experiences to match the specific habits and preferences of each behavioral segment. For example, frequent buyers may respond well to loyalty programs, while customers who haven't purchased recently might benefit from re-engagement campaigns. Behavioral segmentation moves beyond static attributes to capture the dynamic relationship between your business and its customers.

1234567891011121314151617181920212223242526272829303132
import pandas as pd from datetime import datetime # Sample transaction log data data = { "customer_id": [1, 1, 2, 2, 2, 3, 3, 4], "order_id": [101, 102, 103, 104, 105, 106, 107, 108], "order_date": [ "2024-01-05", "2024-04-10", "2024-02-20", "2024-03-15", "2024-06-01", "2024-05-12", "2024-06-20", "2024-03-30" ], "order_value": [120, 80, 50, 60, 70, 200, 220, 35] } df = pd.DataFrame(data) df["order_date"] = pd.to_datetime(df["order_date"]) # Reference date for recency calculation today = datetime(2024, 7, 1) # Aggregate behavioral features features = ( df.groupby("customer_id") .agg( purchase_count=("order_id", "count"), avg_order_value=("order_value", "mean"), last_purchase=("order_date", "max") ) .reset_index() ) features["recency_days"] = (today - features["last_purchase"]).dt.days print(features)
copy
123456789101112131415
import numpy as np # Define behavioral segments based on extracted features def assign_segment(row): if row["purchase_count"] >= 3 and row["recency_days"] <= 30: return "Active Loyalist" elif row["purchase_count"] >= 2 and row["recency_days"] <= 90: return "Recent Buyer" elif row["recency_days"] > 90: return "At Risk" else: return "Occasional" features["behavioral_segment"] = features.apply(assign_segment, axis=1) print(features[["customer_id", "behavioral_segment"]])
copy

Behavioral segments are fundamentally different from demographic or rule-based segments. While demographic segmentation organizes customers by static attributes such as age, gender, or location, and rule-based segmentation applies predefined business rules to group customers, behavioral segmentation focuses on how customers actually behave. This means you are segmenting based on patterns like how often someone buys, how much they spend, or how recently they have interacted with your business. As a result, behavioral segments are dynamic and can change as customer actions change, making them especially valuable for real-time targeting and personalized engagement strategies.

question mark

Which of the following features is most appropriate for behavioral segmentation?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 2

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

Can you explain how the behavioral segments are determined in the code?

What are some practical ways to use these behavioral segments in marketing?

How does behavioral segmentation compare to demographic segmentation in terms of effectiveness?

bookBehavioral Segmentation with Python

Desliza para mostrar el menú

Behavioral segmentation is a powerful technique for understanding how customers interact with your business based on their actions, rather than just their characteristics. By analyzing behavioral patterns—such as purchase frequency, average order value, and recency of transactions—you can group customers according to how they engage with your product or service. This approach enables more precise personalization and targeting, as you can tailor communications, offers, and experiences to match the specific habits and preferences of each behavioral segment. For example, frequent buyers may respond well to loyalty programs, while customers who haven't purchased recently might benefit from re-engagement campaigns. Behavioral segmentation moves beyond static attributes to capture the dynamic relationship between your business and its customers.

1234567891011121314151617181920212223242526272829303132
import pandas as pd from datetime import datetime # Sample transaction log data data = { "customer_id": [1, 1, 2, 2, 2, 3, 3, 4], "order_id": [101, 102, 103, 104, 105, 106, 107, 108], "order_date": [ "2024-01-05", "2024-04-10", "2024-02-20", "2024-03-15", "2024-06-01", "2024-05-12", "2024-06-20", "2024-03-30" ], "order_value": [120, 80, 50, 60, 70, 200, 220, 35] } df = pd.DataFrame(data) df["order_date"] = pd.to_datetime(df["order_date"]) # Reference date for recency calculation today = datetime(2024, 7, 1) # Aggregate behavioral features features = ( df.groupby("customer_id") .agg( purchase_count=("order_id", "count"), avg_order_value=("order_value", "mean"), last_purchase=("order_date", "max") ) .reset_index() ) features["recency_days"] = (today - features["last_purchase"]).dt.days print(features)
copy
123456789101112131415
import numpy as np # Define behavioral segments based on extracted features def assign_segment(row): if row["purchase_count"] >= 3 and row["recency_days"] <= 30: return "Active Loyalist" elif row["purchase_count"] >= 2 and row["recency_days"] <= 90: return "Recent Buyer" elif row["recency_days"] > 90: return "At Risk" else: return "Occasional" features["behavioral_segment"] = features.apply(assign_segment, axis=1) print(features[["customer_id", "behavioral_segment"]])
copy

Behavioral segments are fundamentally different from demographic or rule-based segments. While demographic segmentation organizes customers by static attributes such as age, gender, or location, and rule-based segmentation applies predefined business rules to group customers, behavioral segmentation focuses on how customers actually behave. This means you are segmenting based on patterns like how often someone buys, how much they spend, or how recently they have interacted with your business. As a result, behavioral segments are dynamic and can change as customer actions change, making them especially valuable for real-time targeting and personalized engagement strategies.

question mark

Which of the following features is most appropriate for behavioral segmentation?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 2
some-alt