Last active
May 3, 2019 14:52
-
-
Save karamanbk/361dc69bb1ec14321eaf79086c56b52f 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
#create a dataframe contaning CustomerID and first purchase date | |
tx_min_purchase = tx_uk.groupby('CustomerID').InvoiceDate.min().reset_index() | |
tx_min_purchase.columns = ['CustomerID','MinPurchaseDate'] | |
tx_min_purchase['MinPurchaseYearMonth'] = tx_min_purchase['MinPurchaseDate'].map(lambda date: 100*date.year + date.month) | |
#merge first purchase date column to our main dataframe (tx_uk) | |
tx_uk = pd.merge(tx_uk, tx_min_purchase, on='CustomerID') | |
tx_uk.head() | |
#create a column called User Type and assign Existing | |
#if User's First Purchase Year Month before the selected Invoice Year Month | |
tx_uk['UserType'] = 'New' | |
tx_uk.loc[tx_uk['InvoiceYearMonth']>tx_uk['MinPurchaseYearMonth'],'UserType'] = 'Existing' | |
#calculate the Revenue per month for each user type | |
tx_user_type_revenue = tx_uk.groupby(['InvoiceYearMonth','UserType'])['Revenue'].sum().reset_index() | |
#filtering the dates and plot the result | |
tx_user_type_revenue = tx_user_type_revenue.query("InvoiceYearMonth != 201012 and InvoiceYearMonth != 201112") | |
plot_data = [ | |
go.Scatter( | |
x=tx_user_type_revenue.query("UserType == 'Existing'")['InvoiceYearMonth'], | |
y=tx_user_type_revenue.query("UserType == 'Existing'")['Revenue'], | |
name = 'Existing' | |
), | |
go.Scatter( | |
x=tx_user_type_revenue.query("UserType == 'New'")['InvoiceYearMonth'], | |
y=tx_user_type_revenue.query("UserType == 'New'")['Revenue'], | |
name = 'New' | |
) | |
] | |
plot_layout = go.Layout( | |
xaxis={"type": "category"}, | |
title='New vs Existing' | |
) | |
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