简体   繁体   English

Python Pandas - 如何添加总行以对某些列求和并为其他列求平均值

[英]Python Pandas - how to add a total row to sum certain columns and take the average for others

I have the following code that is working as intended我有以下代码按预期工作

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

Instead of calculating the means for "Repeat Rate" and "NewCust Rate" as the code is doing now, how can I formulas so that the total rows for those columsn are using the following formulas instead:不是像代码现在所做的那样计算“重复率”和“新客户率”的平均值,我如何制定公式,以便这些列的总行数使用以下公式:

Repeat Rate = Table['Existing Customer']/Table2['TotalCusts'] NewCust Rate = Table['New Customer']/Table2['TotalCusts']重复率 = Table['现有客户']/Table2['TotalCusts'] NewCust Rate = Table['New Customer']/Table2['TotalCusts']

Use Index.difference for all columns without specifying in list for sum and columns in list for mean with Series.append for join together:对所有列使用Index.difference而不在列表中指定sum ,将列表中的列指定为mean使用Series.append连接在一起:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (Python)如何获取熊猫中多列之和的平均值 - (Python) How to get the average of the sum of multiple columns in pandas 如何按特定列分组然后计算不是 NA 的多列的计数并将它们添加到 Pandas Python 中? - How to group by certain column then take the count of multiple columns where it is not NA and add them in Pandas Python? 如何在 pandas 中找到多列的总和和平均值 - how to find the sum and average of multiple columns in pandas Pandas:添加总行的最佳方法,该行计算特定(多)列的总和,同时保留数据类型 - Pandas: best way to add a total row that calculates the sum of specific (multiple) columns while preserving the data type 我如何对某些列取行平均值,同时在我的 dataframe 中保留其他列? - How would I take a row-wise average values for certain columns, while retaining other in my dataframe? 如何取某些行的平均值(python) - how to take the average for certain rows (python) Python Pandas:只保留DataFrame中的某些列,同时保留其他列 - Python Pandas: pivot only certain columns in the DataFrame while keeping others Python:如何在熊猫中将列彼此相乘? - Python: how multiply columns with each others in pandas? Python Pandas - 如何添加过滤总和的列并计算百分比权重 - Python Pandas - how to add columns of filtered sum and calculate percentage weight Python Pandas,是否连续检查某些列的条件? - Python Pandas, check the condition of certain columns in a row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM