Last active
November 10, 2020 23:03
-
-
Save egemenzeytinci/e4992df7bf10316354a21ffa4ed411fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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