Solution: Customer Lifetime Value
Walk-through the solution to the Customer Lifetime Value challenge.
Press + to interact
# Task 1.1: Load the cdnow dataset and convert the date datatype# Write your code heredf_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 heredf_cdnow = df_cdnow.groupby('customer_id').agg({'date': ['min', 'max', 'nunique'],'price': 'sum'})# Task 1.4: Rename columns for better manage# Write your code heredf_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 ...