Get startedGet started for free

Building RFM segments

1. Building RFM segments

Congratulations! You have now calculated recency, frequency and monetary values for each customer. We will now group customers into 4 segments of same size for each RFM value. We will use the same approach we did with the dummy data in the previous lesson.

2. Data

We have previously created a dataset containing Recency, Frequency and Monetary Value metrics for each customer. Now our next step will be to use them to assign quartiles of these metrics for each customer.

3. Recency quartile

In order to do that We will first create a generator of values for labels with the range() function. When generating labels we will have to decide whether the high value of the metric should be good or bad, and appropriately define the order of the labels. Since the recency value measures the days since last transaction, we will rate customers who have been active more recently better than the less recent customers. We pass these labels to the qcut() function which sorts the customers by their recency values in an increasing order and then assigns values from 4 to 1 based on the quartile they fall to. Finally, we assign these values to a new column called R.

4. Frequency and monetary quartiles

Now, we do the same thing for Frequency and Monetary Value. The first difference is that the labels have a different order than recency, because frequency and monetary values are considered better when they are higher: we want customers to spend more and visit more often. Hence, we assign higher labels to higher values. Then - as in the previous use case - we pass the column, together with the number of equally sized groups and the labels to the qcut() function. Finally, we create two new more columns F and M for Frequency and MonetaryValue quartiles. And there we have it - all customers have a quartile value assigned that we will use to build the segmentation.

5. Build RFM segment and RFM score

Now the final step is to create the RFM Segment which is just a concatenated string of RFM values, and the RFM Score which is the sum of RFM values. First, we will define a join_rfm() function that creates the concatenated list from RFM values converted to strings. Then we will create RFM_Segment by applying this function to the dataset across the columns. And finally we will create the RFM_Score by summing RFM values across the columns.

6. Final result

This is the result we get - customers get 2 different variables that we created - an RFM segment based from three different RFM values, and the RFM score that sums up the RFM values and indicates a relative customer value.

7. Let's practice building RFM segments

Great job! now you will practice in building RFM segments and calculating RFM scores.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.