Behavioral 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.
1234567891011121314151617181920212223242526272829303132import 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)
123456789101112131415import 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"]])
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.
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
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?
Awesome!
Completion rate improved to 5.56
Behavioral Segmentation with Python
Svep för att visa menyn
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.
1234567891011121314151617181920212223242526272829303132import 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)
123456789101112131415import 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"]])
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.
Tack för dina kommentarer!