in [ ]customer_monetary_val cs_df[['customerid''amount']groupby("customerid"sum(reset_index(customer_history_df customer_history_df merge(customer_monetary_valhow='outer'customer_history_df amount customer_history_df amount+ customer_freq cs_df[['customerid''amount']groupby("customerid"count(reset_index(customer_freq rename(columns={'amount':'frequency'},inplace=truecustomer_history_df customer_history_df merge(customer_freqhow='outer'the input dataframe for clustering will look like the dataframe depicted in figure - notice that we have added small figure to the customer monetary valueas we will be transforming our values to the log scale and presence of zeroes in our data may lead to an error figure - customer value dataframe data preprocessing once we have created our customer value dataframewe will perform some preprocessing on the data for our clusteringwe will be using the -means clustering algorithmwhich we discussed in the earlier one of the requirements for proper functioning of the algorithm is the mean centering of the variable values mean centering of variable value means that we will replace the actual value of the variable with standardized valueso that the variable has mean of and variance of this ensures that all the variables are in the same range and the difference in ranges of values doesn' cause the algorithm to not perform well this is akin to feature scaling another problem that you can investigate about is the huge range of values each variable can take this problem is particularly noticeable for the monetary amount variable to take care of this problemwe will transform all the variables on the log scale this transformationalong with the standardizationwill ensure that the input to our algorithm is homogenous set of scaled and transformed values an important point about the data preprocessing step is that sometimes we need it to be reversible in our casewe will have the clustering results in terms of the log transformed and scaled variable but to make inferences in terms of the original datawe will need to reverse transform all the variable so that we get back the actual rfm figures this can be done by using the preprocessing capabilities of python in [ ]from sklearn import preprocessing import math customer_history_df['recency_log'customer_history_df['recency'apply(math logcustomer_history_df['frequency_log'customer_history_df['frequency'apply(math logcustomer_history_df['amount_log'customer_history_df['amount'apply(math logfeature_vector ['amount_log''recency_log','frequency_log'