Skip to content

Instantly share code, notes, and snippets.

@gbushnell
Created October 15, 2020 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbushnell/78c9ee60369f0788bc80ae782469a43d to your computer and use it in GitHub Desktop.
Save gbushnell/78c9ee60369f0788bc80ae782469a43d to your computer and use it in GitHub Desktop.
RFM Helper Functions
#get days since last invoice (Recency)
def get_history(x):
d = data[data['client_id'] == x.client_id]
d_max = max(d['invoice_date'])
t = datetime.today()
y = t - d_max
return y.days
#count number of invoices for client (Frequency)
c_invoices_dict = collections.Counter(data['client_description'])
def get_counts(x):
return c_invoices_dict[x.client_description]
#get total amount of all invoices for client (Monetary Value)
def total_invoices(x):
d = data[data['client_description'] == x.client_description]
total = d.amount.sum()
return total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment