Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sandy4321/5a935282d4dd0515f2fb714cbc40f011 to your computer and use it in GitHub Desktop.
Save Sandy4321/5a935282d4dd0515f2fb714cbc40f011 to your computer and use it in GitHub Desktop.
# Step 1: Convert all interactions to a list
journeys = campaign_data.groupby('customer_id')['channel'].aggregate(
lambda x: x.tolist()).reset_index()
# Step 2: Add last interaction as 1 or 0 event representing activation
activation_results = campaign_data.drop_duplicates('customer_id', keep='last')[['customer_id', 'activation']]
journeys = pd.merge(journeys, activation_results, how='left', on='customer_id')
# Step 3: Add start and end states based on whether customer activated
journeys['path'] = np.where(
journeys['activation'] == 0,
journeys['channel'].apply(lambda x: ["Start"] + x + ["Null"]),
journeys['channel'].apply(lambda x: ["Start"] + x + ["Activation"])
)
journeys = journeys[['customer_id', 'path']]
# Get overall activation rate
total_activations = journeys['path'].apply(lambda x: x[-1]).str.match('Activation').sum()
activation_rate = total_activations / journeys.shape[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment