Calculate RFM values
Calculate Recency, Frequency and Monetary values for the online dataset we have used before - it has been loaded for you with recent 12 months of data. There's a TotalSum column in the online dataset which has been calculated by multiplying Quantity and UnitPrice: online['Quantity'] * online['UnitPrice'].
Also, we have created a snapshot_date variable that you can use to calculate recency. Feel free to print the online dataset and the snapshot_date into the Console. The pandas library is loaded as pd, and datetime as dt.
Cet exercice fait partie du cours
Customer Segmentation in Python
Instructions
- Calculate Recency, Frequency and Monetary value (in this order) for each customer.
- For Recency - calculate the difference between
snapshot_datevalue and themaxof theInvoiceDate. - Rename the columns as Recency, Frequency and MonetaryValue.
- Print the header with the top 5 rows of the
datamart.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# Calculate Recency, Frequency and Monetary value for each customer
datamart = online.groupby(['CustomerID']).agg({
'InvoiceDate': lambda x: (snapshot_date - x.____()).days,
'InvoiceNo': '____',
'TotalSum': '____'})
# Rename the columns
datamart.rename(columns={'InvoiceDate': '____',
'InvoiceNo': '____',
'TotalSum': '____'}, inplace=True)
# Print top 5 rows
print(____.head())