Skip to content
Course Notes: Customer Segmentation in Python
Course Notes
Use this workspace to take notes, store code snippets, or build your own interactive cheatsheet! The datasets used in this course are available in the datasets
folder.
# Import any packages you want to use here
import pandas as pd
import numpy as np
Take Notes
Add notes here about the concepts you've learned and code cells with code you want to keep.
Add your notes here
# Calculate Recency, Frequency and Monetary value for each customer
datamart = online.groupby(['CustomerID']).agg({
'InvoiceDate': lambda x: (snapshot_date - x.max()).days,
'InvoiceNo': 'count',
'TotalSum': 'sum'})
# Rename the columns
datamart.rename(columns={'InvoiceDate': 'Recency',
'InvoiceNo': 'Frequency',
'TotalSum': 'MonetaryValue'}, inplace=True)
# Print top 5 rows
print(datamart.head())