Data analysis

In order to calculate the CLV, we need to know the frequency, recency, and total amount of purchases by each customer. We are going to compute basic information about each customer's average and lifetime purchase amount, as well as each customer's duration and frequency of purchases. Take a look at the following code:

def groupby_mean(x):    return x.mean()def groupby_count(x):    return x.count()def purchase_duration(x):    return (x.max() - x.min()).daysdef avg_frequency(x):    return (x.max() - x.min()).days/x.count()groupby_mean.__name__ = 'avg'groupby_count.__name__ = 'count'purchase_duration.__name__ = 'purchase_duration'avg_frequency.__name__ = 'purchase_frequency'summary_df = orders_df.reset_index().groupby('CustomerID').agg({

Get Hands-On Data Science for Marketing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.