gpt4 book ai didi

Python Pandas - 总行的自定义计算

转载 作者:太空宇宙 更新时间:2023-11-03 20:02:17 24 4
gpt4 key购买 nike

我有以下代码,它可以按预期工作,但有一个异常(exception)

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())

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

最佳答案

IIUC,只需在两列之间添加直接除法即可。假设 col xcol y 属于 Table2

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

关于Python Pandas - 总行的自定义计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59170655/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com