...

/

Solution: Customer Lifetime Value

Solution: Customer Lifetime Value

Walk-through the solution to the Customer Lifetime Value challenge.

We'll cover the following...

Task 1

Here is the solution to data preparation for CLV calculation.

Press + to interact
# Task 1.1: Load the cdnow dataset and convert the date datatype
# Write your code here
df_cdnow = pd.read_csv('cdnow.csv', index_col=0)
df_cdnow['date'] = pd.to_datetime(df_cdnow['date'])
# Perform task 1.2 and 1.3 together with groupby() function
# Task 1.2: Identify first and last transaction date, also number of transactions
# Task 1.3: Aggregate total transaction value
# Write your code here
df_cdnow = df_cdnow.groupby('customer_id').agg({
'date': ['min', 'max', 'nunique'],
'price': 'sum'
})
# Task 1.4: Rename columns for better manage
# Write your code here
df_cdnow.columns = ['_'.join(col).strip() for col in df_cdnow.columns.values]
df_cdnow.rename(columns={
'date_min': 'first_transaction',
'date_max': 'last_transaction',
'date_nunique': 'frequency',
'price_sum': 'revenue'
}, inplace=True)
print(df_cdnow.head())

Explanation ...