Pandas User Behavior Analysis in Action
User behavior analysis is an important basis for product optimization. This case demonstrates how to analyze user access, retention, conversion, and other behavioral data.
Data Preparation
Simulate user behavior data
Example
import pandas as pd
import numpy as np
np.random.seed(42)
n_users = 500
n_events = 5000
# User basic information
users = pd.DataFrame({
"User ID": range(1, n_users + 1),
"Registration Date": pd.date_range("2024-01-01", periods=n_users, freq="D")[:n_users],
"Channel": np.random.choice(["Organic Search", "Advertising", "Referral", "Social"], n_users),
"Device": np.random.choice(["PC", "Mobile", "Tablet"], n_users, p=[0.3, 0.6, 0.1])
})
# User behavior events
events = pd.DataFrame({
"Event ID": range(1, n_events + 1),
"User ID": np.random.randint(1, n_users + 1, n_events),
"Event Type": np.random.choice(
["View", "Click", "Favorite", "Add to Cart", "Order", "Comment"],
n_events,
p=[0.4, 0.25, 0.1, 0.1, 0.1, 0.05]
),
"Event Time": pd.date_range("2024-01-15", periods=n_events, freq="10min")
})
# Merge data
events = events.merge(users[["User ID", "Channel", "Device"]], on="User ID")
print("User data:")
print(users.head())
print(f"\nNumber of users: {len(users)}")
print("\n"Event data:")
print(events.head())
print(f"\nNumber of events: {len(events)}")
import numpy as np
np.random.seed(42)
n_users = 500
n_events = 5000
# User basic information
users = pd.DataFrame({
"User ID": range(1, n_users + 1),
"Registration Date": pd.date_range("2024-01-01", periods=n_users, freq="D")[:n_users],
"Channel": np.random.choice(["Organic Search", "Advertising", "Referral", "Social"], n_users),
"Device": np.random.choice(["PC", "Mobile", "Tablet"], n_users, p=[0.3, 0.6, 0.1])
})
# User behavior events
events = pd.DataFrame({
"Event ID": range(1, n_events + 1),
"User ID": np.random.randint(1, n_users + 1, n_events),
"Event Type": np.random.choice(
["View", "Click", "Favorite", "Add to Cart", "Order", "Comment"],
n_events,
p=[0.4, 0.25, 0.1, 0.1, 0.1, 0.05]
),
"Event Time": pd.date_range("2024-01-15", periods=n_events, freq="10min")
})
# Merge data
events = events.merge(users[["User ID", "Channel", "Device"]], on="User ID")
print("User data:")
print(users.head())
print(f"\nNumber of users: {len(users)}")
print("\n"Event data:")
print(events.head())
print(f"\nNumber of events: {len(events)}")
User Activity Analysis
Event Type Distribution
Example
# Event type statistics
event_dist = events["Event Type"].value_counts()
event_pct = events["Event Type"].value_counts(normalize=True) * 100
print("=== Event Type Distribution ===\n")
event_analysis = pd.DataFrame({
"Count": event_dist,
"Percentage": event_pct.round(2)
})
print(event_analysis)
print()
# Number of users by event type
event_users = events.groupby("Event Type")["User ID"].nunique()
print("Number of users participating in each event type:")
print(event_users)
event_dist = events["Event Type"].value_counts()
event_pct = events["Event Type"].value_counts(normalize=True) * 100
print("=== Event Type Distribution ===\n")
event_analysis = pd.DataFrame({
"Count": event_dist,
"Percentage": event_pct.round(2)
})
print(event_analysis)
print()
# Number of users by event type
event_users = events.groupby("Event Type")["User ID"].nunique()
print("Number of users participating in each event type:")
print(event_users)
User Engagement
Example
# User event statistics
user_events = events.groupby("User ID").agg({
"Event ID": "count",
"Event Type": lambda x: x.nunique()
}).rename(columns={
"Event ID": "Total Events",
"Event Type": "Number of Event Types"
})
print("=== User Engagement ===\n")
print(f"Average events per user: {user_events['Total Events'].mean():.1f}")
print(f"Median: {user_events['Total Events'].median():.0f}")
print(f"Max: {user_events['Total Events'].max()}")
print()
# User segmentation
user_events["Activity Level"] = pd.cut(
user_events["Total Events"],
bins=[0, 3, 10, 20, float("inf")],
labels=["Low Activity", "Normal", "Active", "High Activity"]
)
print("User activity level distribution:")
print(user_events["Activity Level"].value_counts())
user_events = events.groupby("User ID").agg({
"Event ID": "count",
"Event Type": lambda x: x.nunique()
}).rename(columns={
"Event ID": "Total Events",
"Event Type": "Number of Event Types"
})
print("=== User Engagement ===\n")
print(f"Average events per user: {user_events['Total Events'].mean():.1f}")
print(f"Median: {user_events['Total Events'].median():.0f}")
print(f"Max: {user_events['Total Events'].max()}")
print()
# User segmentation
user_events["Activity Level"] = pd.cut(
user_events["Total Events"],
bins=[0, 3, 10, 20, float("inf")],
labels=["Low Activity", "Normal", "Active", "High Activity"]
)
print("User activity level distribution:")
print(user_events["Activity Level"].value_counts())
Conversion Funnel Analysis
Conversion Path
Example
# Count users by event type
funnel = events.groupby("Event Type")["User ID"].nunique()
funnel = funnel.reindex(["View", "Click", "Favorite", "Add to Cart", "Order", "Comment"])
# Calculate conversion rates
funnel_df = pd.DataFrame({
"Users": funnel,
"Absolute Conversion Rate": (funnel / funnel["View"] * 100).round(2),
"Step Conversion Rate": (funnel / funnel.shift(1) * 100).round(2)
}).fillna(100)
print("=== Conversion Funnel ===\n")
print(funnel_df)
print()
# Visualize funnel data
print("Funnel data:")
for step, row in funnel_df.iterrows():
print(f"{step}: {row['Users']} users ({row['Absolute Conversion Rate']:.1f}%)")
funnel = events.groupby("Event Type")["User ID"].nunique()
funnel = funnel.reindex(["View", "Click", "Favorite", "Add to Cart", "Order", "Comment"])
# Calculate conversion rates
funnel_df = pd.DataFrame({
"Users": funnel,
"Absolute Conversion Rate": (funnel / funnel["View"] * 100).round(2),
"Step Conversion Rate": (funnel / funnel.shift(1) * 100).round(2)
}).fillna(100)
print("=== Conversion Funnel ===\n")
print(funnel_df)
print()
# Visualize funnel data
print("Funnel data:")
for step, row in funnel_df.iterrows():
print(f"{step}: {row['Users']} users ({row['Absolute Conversion Rate']:.1f}%)")
Channel Analysis
Performance by Channel
Example
# Channel user analysis
channel_analysis = events.groupby("Channel").agg({
"User ID": "nunique",
"Event ID": "count"
}).rename(columns={
"User ID": "Users",
"Event ID": "Events"
})
channel_analysis["Events per User"] = (
channel_analysis["Events"] / channel_analysis["Users"]
).round(2)
print("=== Channel Analysis ===\n")
print(channel_analysis)
print()
# Channel conversion comparison
channel_funnel = events[events["Event Type"].isin(["View", "Order"])].groupby(
["Channel", "Event Type"]
)["User ID"].nunique().unstack()
channel_funnel["Conversion Rate"] = (
channel_funnel["Order"] / channel_funnel["View"] * 100
).round(2)
print("Conversion rate by channel:")
print(channel_funnel)
channel_analysis = events.groupby("Channel").agg({
"User ID": "nunique",
"Event ID": "count"
}).rename(columns={
"User ID": "Users",
"Event ID": "Events"
})
channel_analysis["Events per User"] = (
channel_analysis["Events"] / channel_analysis["Users"]
).round(2)
print("=== Channel Analysis ===\n")
print(channel_analysis)
print()
# Channel conversion comparison
channel_funnel = events[events["Event Type"].isin(["View", "Order"])].groupby(
["Channel", "Event Type"]
)["User ID"].nunique().unstack()
channel_funnel["Conversion Rate"] = (
channel_funnel["Order"] / channel_funnel["View"] * 100
).round(2)
print("Conversion rate by channel:")
print(channel_funnel)
Device Analysis
Example
# Device analysis
device_analysis = events.groupby("Device").agg({
"User ID": "nunique",
"Event ID": "count"
}).rename(columns={
"User ID": "Users",
"Event ID": "Events"
})
device_analysis["Events per User"] = (
device_analysis["Events"] / device_analysis["Users"]
).round(2)
print("=== Device Distribution ===\n")
print(device_analysis)
print()
# Device conversion rate
device_funnel = events[events["Event Type"].isin(["View", "Order"])].groupby(
["Device", "Event Type"]
)["User ID"].nunique().unstack()
device_funnel["Conversion Rate"] = (
device_funnel["Order"] / device_funnel["View"] * 100
).round(2)
print("Conversion rate by device:")
print(device_funnel)
device_analysis = events.groupby("Device").agg({
"User ID": "nunique",
"Event ID": "count"
}).rename(columns={
"User ID": "Users",
"Event ID": "Events"
})
device_analysis["Events per User"] = (
device_analysis["Events"] / device_analysis["Users"]
).round(2)
print("=== Device Distribution ===\n")
print(device_analysis)
print()
# Device conversion rate
device_funnel = events[events["Event Type"].isin(["View", "Order"])].groupby(
["Device", "Event Type"]
)["User ID"].nunique().unstack()
device_funnel["Conversion Rate"] = (
device_funnel["Order"] / device_funnel["View"] * 100
).round(2)
print("Conversion rate by device:")
print(device_funnel)
Retention Analysis
Example
# Simplified retention analysis
# Assume active within 7 days after registration is considered retention
users_with_events = events.groupby("User ID")["Event Time"].agg(["min", "max"])
users_with_events.columns = ["First Active", "Last Active"]
# Merge registration info
users_analysis = users.merge(users_with_events, left_on="User ID", right_index=True, how="left")
# Calculate active days
users_analysis["Active Days"] = (
users_analysis["Last Active"] - users_analysis["First Active"]
).dt.days + 1
print("=== User Retention Analysis ===\n")
print(f"Users with behavior: {len(users_with_events)}")
print(f"Average active days: {users_analysis['Active Days'].mean():.1f} days")
print()
# Next-day retention (simplified)
retention_1d = len(users_analysis[users_analysis["Active Days"] >= 2]) / len(users_analysis) * 100
retention_3d = len(users_analysis[users_analysis["Active Days"] >= 3]) / len(users_analysis) * 100
print(f"Next-day retention rate: {retention_1d:.1f}%")
print(f"3-day retention rate: {retention_3d:.1f}%")
# Assume active within 7 days after registration is considered retention
users_with_events = events.groupby("User ID")["Event Time"].agg(["min", "max"])
users_with_events.columns = ["First Active", "Last Active"]
# Merge registration info
users_analysis = users.merge(users_with_events, left_on="User ID", right_index=True, how="left")
# Calculate active days
users_analysis["Active Days"] = (
users_analysis["Last Active"] - users_analysis["First Active"]
).dt.days + 1
print("=== User Retention Analysis ===\n")
print(f"Users with behavior: {len(users_with_events)}")
print(f"Average active days: {users_analysis['Active Days'].mean():.1f} days")
print()
# Next-day retention (simplified)
retention_1d = len(users_analysis[users_analysis["Active Days"] >= 2]) / len(users_analysis) * 100
retention_3d = len(users_analysis[users_analysis["Active Days"] >= 3]) / len(users_analysis) * 100
print(f"Next-day retention rate: {retention_1d:.1f}%")
print(f"3-day retention rate: {retention_3d:.1f}%")
Analysis Summary
Example
print("""
=== User Behavior Analysis Summary ===
1. User Engagement
- Event types are dominated by views, followed by clicks
- The average number of events per user is low; user stickiness needs to be improved
2. Conversion Funnel
- There is significant drop-off; user experience needs to be optimized
- The conversion rate from view to order is relatively low
3. Channel Effectiveness
- It is recommended to focus on optimizing the conversion path of high-traffic channels
- Performance varies across channels; refined operations are needed
4. Device Performance
- Mobile is the primary device; the mobile experience needs to be optimized
5. Optimization Suggestions
- 1) Optimize product display to improve view-to-click conversion
- 2) Simplify the shopping process to reduce order drop-off
- 3) Optimize page load speed for mobile
- 4) Establish a user tiered operation system
""")
=== User Behavior Analysis Summary ===
1. User Engagement
- Event types are dominated by views, followed by clicks
- The average number of events per user is low; user stickiness needs to be improved
2. Conversion Funnel
- There is significant drop-off; user experience needs to be optimized
- The conversion rate from view to order is relatively low
3. Channel Effectiveness
- It is recommended to focus on optimizing the conversion path of high-traffic channels
- Performance varies across channels; refined operations are needed
4. Device Performance
- Mobile is the primary device; the mobile experience needs to be optimized
5. Optimization Suggestions
- 1) Optimize product display to improve view-to-click conversion
- 2) Simplify the shopping process to reduce order drop-off
- 3) Optimize page load speed for mobile
- 4) Establish a user tiered operation system
""")