Created
August 18, 2019 10:00
-
-
Save FBosler/8767013a67a9ea5814ff3f1229ed4915 to your computer and use it in GitHub Desktop.
functions extracted from main function for simplicity
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
def _generate_cohorts(dataset, metric): | |
cohorts = dataset.groupby(['cohort','order_period']).agg({ | |
'order_id':pd.Series.nunique, | |
'order_size':sum, | |
'basket_size':sum | |
}) | |
cohorts.columns = ['number_of_orders','number_of_items_bought','total_order_value'] | |
cohorts = cohorts[metric].unstack(0) | |
return cohorts | |
def _generate_repeat_percentages(dataset,metric): | |
repeat_perc = dataset.groupby(['cohort', 'type_of_order']).agg({ | |
'order_id':pd.Series.nunique, | |
'order_size':sum, | |
'basket_size':sum | |
}).unstack() | |
repeat_perc = repeat_perc.stack().T.stack(level=0).fillna(0) | |
repeat_perc['percentage repeat'] = repeat_perc['repeat']/repeat_perc.sum(axis=1) | |
repeat_perc = repeat_perc.unstack(level=0).iloc[:,-3:] | |
repeat_perc.columns = ['Orders Repeat %', 'Items Bought Repeat %', 'Order Value Repeat %'] | |
if metric == 'number_of_orders': | |
selection = 'Orders Repeat %' | |
if metric == 'number_of_items_bought': | |
selection = 'Items Bought Repeat %' | |
if metric == 'total_order_value': | |
selection = 'Order Value Repeat %' | |
if not selection: | |
raise NotImplementedError('No repeat figures for specified metric') | |
repeat_perc = repeat_perc[selection].reset_index() | |
return repeat_perc, selection |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment