首页 > 解决方案 > Python Pandas - 总行的自定义计算

问题描述

我有以下代码可以按需要工作,但有一个例外

df['FPYear'] = df['First_Purchase_Date'].dt.year

Table2 = df.loc[df.Date.between('2014-01-01','2019-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'] == 'USA')].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())

print(Table2)

对于代码的最后一行,

Table2.loc['Total'] = Table2[diff_cols].sum().append(Table2[cols].mean())

我宁愿添加客户函数(简单的 col x/col y),而不是让它取所有其他列的 mean(),但是在尝试了一些不同的事情之后,我无法这样做。

标签: pythonpandasgroup-by

解决方案


IIUC,只需在 2 列之间添加直接划分。假设col x并且col y属于Table2

Table2.loc['Total'] = Table2[diff_cols].sum().append(Table2['col x'] / Table2['col y'])

推荐阅读