Skip to content

Instantly share code, notes, and snippets.

@egemenzeytinci
Last active November 10, 2020 23:03
Show Gist options
  • Save egemenzeytinci/e4992df7bf10316354a21ffa4ed411fe to your computer and use it in GitHub Desktop.
Save egemenzeytinci/e4992df7bf10316354a21ffa4ed411fe to your computer and use it in GitHub Desktop.
# three quantiles to rfm values
df['r_val'] = pd.qcut(df['recency'], q=3, labels=range(3, 0, -1))
df['f_val'] = pd.qcut(df['frequency'], q=3, labels=range(1, 4))
df['m_val'] = pd.qcut(df['monetary'], q=3, labels=range(1, 4))
# create the segment value
df['rfm_val'] = (
df['r_val'].astype(str) +
df['f_val'].astype(str) +
df['m_val'].astype(str)
)
# example names for segments
mapping = {
'Best customers': '333',
'No purchases recently': '133',
'Low loyalty': '111',
'New customers': '311'
}
# print the results
for k, v in mapping.items():
print(k + ',')
print(df[df.rfm_val == v].drop('customer', axis=1).describe().T)
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment