我有下面的代码,这是工作的预期
df['FPYear'] = df['First_Purchase_Date'].dt.year
# Table2 = df.loc[df.Date.between('2018-11-22','2018-11-30')].groupby(df['FPYear'])[['New Customer', 'Existing Customer', 'revenue']].sum() #with date filters for table
Table2 = df.loc[df.Date.between('2018-11-22','2018-11-30') & (df['Region'] == 'Canada')].groupby(df['FPYear'])[['New Customer', 'Existing Customer', 'revenue']].sum() #with date filters for table
Table2['TotalCusts'] = Table2['New Customer'] + Table2['Existing Customer']
Table2['Cohort Size'] = Table['New Customer']
Table2['Repeat Rate'] = Table2['Existing Customer']/Table2['TotalCusts']
Table2['NewCust Rate'] = Table2['New Customer']/Table2['TotalCusts']
Table2['PCT of Total Yr'] = Table2['TotalCusts']/Table['New Customer']
Table2.loc['Total'] = Table2.sum(axis = 0) this code totals all columns. #the below calcs totals for some and average for others
cols = ["Repeat Rate", "NewCust Rate"]
diff_cols = Table2.columns.difference(cols)
Table2.loc['Total'] = Table2[diff_cols].sum().append(Table2[cols].mean())
与代码现在所做的计算“Repeat Rate”和“NewCust Rate”的平均值不同,如何使用公式使这些列的总行使用以下公式:
重复率=表['Existing Customer']/表2['TotalCusts'] NewCust Rate=Table['New Customer']/表2['TotalCusts']
对所有列使用^{} ,而不在列表中指定} 进行联接:
sum
,对列表中的列使用mean
和^{相关问题 更多 >
编程相关推荐