python - 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(),但是在尝试了一些不同的事情之后,我无法这样做。
解决方案
IIUC,只需在 2 列之间添加直接划分。假设col x
并且col y
属于Table2
Table2.loc['Total'] = Table2[diff_cols].sum().append(Table2['col x'] / Table2['col y'])
推荐阅读
- c++ - 试图发送任何函数作为参数并推断类型?
- qt - QT QML 在运行时不翻译文本集
- java - 拉普拉斯金字塔产生奇怪的结果?
- python - 朴素贝叶斯分类器的 scikit-learn 文档中的示例代码错误?
- sas - 使用逗号分隔符 SAS 在字符串中的前两个单词上左连接
- cmd - 端口 23 上的 telnet 连接失败
- db2-400 - DB2/400:BLOB abd GRAPHIC DB2/400 属性之间的区别?
- c# - ClientWebSocket 的 ReceiveAsync 中的 ObjectDisposedException
- android - GoogleMap 使用协程暂停初始化程序
- javascript - Vue.js 在现有应用程序上的电子