Created
May 3, 2019 19:27
-
-
Save karamanbk/2a5b1f047af307a858d8de79562bd5b0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#create a generic user dataframe to keep CustomerID and new segmentation scores | |
tx_user = pd.DataFrame(tx_data['CustomerID'].unique()) | |
tx_user.columns = ['CustomerID'] | |
#get the max purchase date for each customer and create a dataframe with it | |
tx_max_purchase = tx_uk.groupby('CustomerID').InvoiceDate.max().reset_index() | |
tx_max_purchase.columns = ['CustomerID','MaxPurchaseDate'] | |
#we take our observation point as the max invoice date in our dataset | |
tx_max_purchase['Recency'] = (tx_max_purchase['MaxPurchaseDate'].max() - tx_max_purchase['MaxPurchaseDate']).dt.days | |
#merge this dataframe to our new user dataframe | |
tx_user = pd.merge(tx_user, tx_max_purchase[['CustomerID','Recency']], on='CustomerID') | |
tx_user.head() | |
#plot a recency histogram | |
plot_data = [ | |
go.Histogram( | |
x=tx_user['Recency'] | |
) | |
] | |
plot_layout = go.Layout( | |
title='Recency' | |
) | |
fig = go.Figure(data=plot_data, layout=plot_layout) | |
pyoff.iplot(fig) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment