事情是这样的,我正在构建一个流光应用程序来获取队列数据。就像这里解释的:https://towardsdatascience.com/a-step-by-step-introduction-to-cohort-analysis-in-python-a2cbbd8460ea。所以,基本上,我现在有一个数据,有一个队列日期(队列),属于这个队列的客户数量,并且在那个月(n_customers)和付款月份(订单月份)购买。现在,我必须得到一个关于句号的列。我的意思是,我有这个:
cohort order_month n_customers
2009-12 2009-12 1045
2009-12 2010-01 392
2009-12 2010-02 358
.
.
.我想要得到这个:
cohort order_month n_customers period_number
2009-12 2009-12 1045 0
2009-12 2010-01 392 1
2009-12 2010-02 358 2
.
.
.dataframe的名称是df_cohort。
因此,在2009年12月12日,有1045名来自12/2009的客户购买了一些东西。在2010年01月,有392名来自12/2009的客户购买产品。诸若此类。我需要创建列period_number来构建我的热图。
我试过运行这个:
df_cohort["period_number"] = (
df_cohort - df_cohort
).apply(attrgetter("n"))但我发现了一个错误:
AttributeError: 'Timedelta' object has no attribute 'n'我需要构建与教程有点不同的数据格式,这就是为什么我有这个错误。从现在开始我能解决这个问题吗?不会改变以前的东西,但只会从这个开始。
对于每一列的数据类型,order_month和corhort都是datetime64ns。
发布于 2022-04-29 12:00:58
您试过指定列吗?喜欢
df_cohort['period_number'] = (df_cohort['invoice_month']-df_cohort['cohort']).apply(attrgetter('n'))谢谢。
发布于 2022-04-29 12:09:02
您可以尝试应用创建这些句点的函数,例如
def cohort_period(df):
df['CohortPeriod'] = np.arange(len(df))+1
return df
cohorts = cohorts.groupby(level=0).apply(cohort_period)https://stackoverflow.com/questions/67558323
复制相似问题