Mission 468-9: Investigating Churn

Hello,

I was wondering if someone could post here the code for generating the graphs shown on this screen?

I was trying to reconstruct the graphs but am at a loss.

Thanks

Screen Link:

My Code:

What I expected to happen:

Replace this line with the code output you expected to get

What actually happened:

Replace this line with the actual output/error

I was able to (somewhat) reconstruct the third graph “annual subscription”, but not the “monthly subscription” churn. But even though the shape of the graph is the same, the peak has a slightly different value.

subs_annual = subs[subs["subscription_period"] == "annual"] 

subs_annual_churn = pd.DataFrame({"total_churned": subs_annual.groupby("churn_month").size()})
churn = pd.DataFrame({"yearmonth": yearmonths})
churn = pd.merge(churn, subs_annual_churn, "left", left_on="yearmonth", right_index=True)
churn.fillna(0, inplace=True)
churn["total_churned"] = churn["total_churned"].astype(int)

def get_customers(yearmonth):
    year = yearmonth//100
    month = yearmonth-year*100
    date = dt.datetime(year, month, 1)
    
    return ((subs["start_date"] < date) & (date <= subs["end_date"])).sum()

churn["total_customers"] = churn["yearmonth"].apply(get_customers)
churn["churn_rate"] = churn["total_churned"] / churn["total_customers"]
churn["yearmonth"] = churn["yearmonth"].astype(str)

arange = __import__("numpy").arange
Ellipse = __import__("matplotlib").patches.Ellipse
ax = churn.plot(x="yearmonth", y="churn_rate", figsize=(12,6), rot=45, marker=".")
start, end = ax.get_xlim()
ax.get_xticks()
ax.set_xticks(arange(2, end, 3))
ax.set_xticklabels(yearmonths[2::3])
# circle = Ellipse((35, churn.loc[churn.yearmonth == "201312", "churn_rate"].iloc[0]),
#                  5, 0.065, color='sandybrown', fill=False
#                    )
# ax.add_artist(circle)
ax.xaxis.label.set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.get_legend().remove()
1 Like

Yes it does show some errors but I can see the graph.
Thank you