Python演示了如何添加一个总计行来对某些列求和,并对其他列取平均值

2024-04-25 21:38:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的代码,这是工作的预期

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']


Tags: dfnewfordateratetablecustomerloc
1条回答
网友
1楼 · 发布于 2024-04-25 21:38:34

对所有列使用^{},而不在列表中指定sum,对列表中的列使用mean^{}进行联接:

cols = ["Repeat Rate", "NewCust Rate"]
diff_cols = Table2.columns.difference(cols)
Table2.loc['Total'] = Table2[diff_cols].sum().append(Table2[cols].mean())

相关问题 更多 >