Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
data_redirects['group'] = "N/A"
data_redirects.loc[data_redirects['true_url'].str.contains(r"/collections(?!.*products.*)(?!.*/product.*)"), "group"] = "Collections"
data_redirects.loc[data_redirects['true_url'].str.contains(r".*/products/.*|.*/product/.*"), "group"] = "Products"
grouped_data = data_redirects[['group', "ga:newUsers", "ga:date"]].groupby(["group", "ga:date"]).sum().reset_index()
# before and after comparison
grouped_before = grouped_data[grouped_data['ga:date'] < pd.to_datetime("2017-12-15")]
grouped_after = grouped_data[grouped_data['ga:date'] >= pd.to_datetime("2017-12-15")]
grouped_before_total = grouped_before[["group", "ga:newUsers"]].groupby("group").sum().reset_index()
grouped_after_total = grouped_after[["group", "ga:newUsers"]].groupby("group").sum().reset_index()
grouped_change = grouped_before_total.merge(grouped_after_total, left_on="group", right_on="group", suffixes=["_before", "_after"], how="outer")
#calculate difference and percentage change
grouped_change['difference'] = grouped_change['ga:newUsers_after'] - grouped_change['ga:newUsers_before']
grouped_change['percent_change'] = grouped_change['difference'] / grouped_change['ga:newUsers_before']
grouped_change.sort_values("difference")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.