cs_df cs_df[~(cs_df amount< )cs_df head(out[ ]invoiceno stockcode description quantity white hanging heart -light holder white metal lantern cream cupid hearts coat hanger knitted union flag hot water bottle red woolly hottie white heart invoicedate unitprice customerid country amount : : united kingdom : : united kingdom : : united kingdom : : united kingdom : : united kingdom the data now has only buying transactions from united kingdom we will now remove all the transactions that have missing value for the customerid field as all our subsequent transactions will be based on the customer entities in [ ]cs_df cs_df[~(cs_df customerid isnull())in [ ]cs_df shape out[ ]( the next step is creating the recencyfrequencyand monetary value features for each of the customers that exist in our dataset recency to create the recency feature variablewe need to decide the reference date for our analysis for our use casewe will define the reference date as one day after the last transaction in our dataset in [ ]refrence_date cs_df invoicedate max(refrence_date refrence_date datetime timedelta(days refrence_date out[ ]timestamp( : : 'we will construct the recency variable as the number of days before the reference date when customer last made purchase the following snippet of code will create this variable for us in [ ]cs_df['days_since_last_purchase'refrence_date cs_df invoicedate cs_df['days_since_last_purchase_num'cs_df['days_since_last_purchase'astype('timedelta [ ]'in [ ]customer_history_df cs_df groupby("customerid"min(reset_index([['customerid''days_since_last_purchase_num']customer_history_df rename(columns={'days_since_last_purchase_num':'recency'}inplace=true